CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2010
    Posts
    75

    static in the class

    whats wrong with my code
    why it doesnt work i got some errors
    it should output in the end x=3 and y=3 in the end of the program

    PHP Code:
    # include <iostream>
    using namespace std;
    void sFune();
    void main()
    { for (
    int i=0i<3i++)
     
    sFune();
    }

    void sFune ()
    {
        static 
    int x=2;
        
    int y=2;
        
    x++;
        
    y++;
        
    cout << " X = "<<x<<endl;
        
    cout << " Y = "<<y<<endl;
    }

        class 
    secretclass

        
    {

        public : 
            
    void set int a)
            {
                
    a;
            }

            
    void printY()
            {
                
    cout <<y<<endl;
            }

            static 
    void PrintNum ()
            {
                
    cout <<num<<endl;
            }

            static 
    void increase(int n)
            {
                
    num+=n;
            }

            
    secretclass int a=0)
            {
                
    y=0;
                
    num ++;
            }

        private :
            
    int y;
            static 
    int num;
        };

        
    int secretclass::num=10;

        
    int main ()
        {
            
    secretclass obj1(50);
            
    secretclass::PrintNum();
            
    secretclass.obj2 (20)
            
    secretclass::PrintNum();
            
    obj1.increase(5);
            
    obj2.increase(2);
            
    obj1.printY();
            
    obj2.printY();
            
    secretclass::increase(10);
            
    obj1.PrintNum();
            return 
    0;
        } 

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: static in the class

    What are the errors?

  3. #3
    Join Date
    Jul 2010
    Posts
    75

    Re: static in the class

    that are the errors

    Error 1 error C2084: function 'int main(void)' already has a body

    Error 2 error C2143: syntax error : missing ';' before '.'

    Error 3 error C2143: syntax error : missing ';' before '.'

    Error 4 error C2065: 'obj2' : undeclared identifier

    Error 5 error C2228: left of '.increase' must have class/struct/union

    Error 6 error C2065: 'obj2' : undeclared identifier

    Error 7 error C2228: left of '.printY' must have class/struct/union

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: static in the class

    Quote Originally Posted by jacksparrow View Post
    that are the errors

    Error 1 error C2084: function 'int main(void)' already has a body
    Well yes, you've defined two different functions called main() in that code snippet. And by the way, main should return int, not void. (The return value of main is the status which tells the system whether or not the program completed successfully.)

    Error 2 error C2143: syntax error : missing ';' before '.'

    Error 3 error C2143: syntax error : missing ';' before '.'

    Error 4 error C2065: 'obj2' : undeclared identifier

    Error 5 error C2228: left of '.increase' must have class/struct/union

    Error 6 error C2065: 'obj2' : undeclared identifier

    Error 7 error C2228: left of '.printY' must have class/struct/union
    [/quote]

    These all stem from the same mistake. Compare the declarations of obj1 and obj2 carefully----they are not the same. One is correct and the other is not.

  5. #5
    Join Date
    Jul 2010
    Posts
    75

    Re: static in the class

    you were right dude ,, thx i fixed the mistakes and at the end there was only one left..i already changed the void main to int main

    Error 1 error C2084: function 'int main(void)' already has a body


    PHP Code:
    # include <iostream>
    using namespace std;
    void sFune();
    int main()
    { for (
    int i=0i<3i++)
     
    sFune();
    }

    void sFune ()
    {
        static 
    int x=2;
        
    int y=2;
        
    x++;
        
    y++;
        
    cout << " X = "<<x<<endl;
        
    cout << " Y = "<<y<<endl;
    }

        class 
    secretClass
        
    {
        public : 
            
    void set int a)
            {
                
    a;
            }

            
    void printY()
            {
                
    cout <<y<<endl;
            }

            static 
    void printNum ()
            {
                
    cout <<num<<endl;
            }

            static 
    void increase(int n)
            {
                
    num+=n;
            }

            
    secretClass int a=0)
            {
                
    y=0;
                
    num ++;
            }

        private :
            
    int y;
            static 
    int num;
        };

        
    int secretClass::num=10;

        
    int main ()
        {
            
            
    secretClass obj1(50);
            
    secretClass::printNum();
            
    secretClass obj2(20);
            
    secretClass::printNum();
            
    obj1.increase(5);
            
    obj2.increase(2);
            
    obj1.printY();
            
    obj2.printY();
            
    secretClass::increase(10);
            
    obj1.printNum();
            return 
    0;
        } 

  6. #6
    Join Date
    Mar 2002
    Location
    Kent, United Kingdom
    Posts
    399

    Re: static in the class

    You still have two main() functions.
    your humble savant

  7. #7
    Join Date
    Jul 2010
    Posts
    75

    Re: static in the class

    i should remove the one below using namespace ?

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: static in the class

    That depends on what your code is really supposed to do. What are you actually trying to do?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: static in the class

    To me this simply looks like two distinct programs in a single file...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured