-
October 25th, 2010, 08:59 AM
#1
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=0; i<3; i++)
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)
{
y = 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;
}
-
October 25th, 2010, 09:02 AM
#2
-
October 25th, 2010, 10:43 AM
#3
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
-
October 25th, 2010, 10:55 AM
#4
Re: static in the class
Originally Posted by jacksparrow
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.
-
October 25th, 2010, 11:11 AM
#5
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=0; i<3; i++) 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) { y = 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; }
-
October 25th, 2010, 11:12 AM
#6
Re: static in the class
You still have two main() functions.
your humble savant
-
October 25th, 2010, 12:25 PM
#7
Re: static in the class
i should remove the one below using namespace ?
-
October 25th, 2010, 12:27 PM
#8
Re: static in the class
That depends on what your code is really supposed to do. What are you actually trying to do?
-
October 25th, 2010, 03:41 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|