C++: Error...x is static....

Started by
16 comments, last by GameDev.net 18 years, 8 months ago
OK....I'll explain my problem to you this way: Let me start my c++ program class theproblem { public: int x int y }; It's OK so far, now let's add some extra stuff to it... class theproblem { public: int x=10 //I've just specified a value to X int y }; Error....X is static. So I am bored again and look at my java manual and the best "hello world" program is this: class helloworld { public static void main(string[] args) { system.out.println("hello world!") } }; How do we make our method "static" like in Java cos otherwise I can never set any values to my variables and many other things will never work... HELP ME! PS...I looked up many tutorials and none of them explained very well.
Advertisement
You havnt got any constructor or destructor, i think....

ace
Quote:Original post by ace_lovegrove
You havnt got any constructor or destructor, i think....

ace


I actually haven't put any of the values in a method so it makes no difference and that wouldn't cause any program errors unless I referred to the destroyed variable later on
post the compile error...
If you want one variable per instance of the class.

Foo.hclass Foo{public:   int x;   Foo() :       x(10) // x is set to 10 when the object is created   {}};


If you want one variable for the whole class (i.e. a static member variable)

Foo.hclass Foo{   static int x;};Foo.cppint Foo::x = 10; // x is set to 10 at program startup
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
If you want one variable per instance of the class.

Foo.hclass Foo{public:   int x;   Foo() :       x(10) // x is set to 10 when the object is created   {}};


If you want one variable for the whole class (i.e. a static member variable)

Foo.hclass Foo{   static int x;};Foo.cppint Foo::x = 10; // x is set to 10 at program startup


I understand...

But when I use cout....it also complains that it isn't "static"...

Quote:Original post by ReneGade RG dev
But when I use cout....it also complains that it isn't "static"...


Show the relevant code. All the relevant code and nothing but the relevant code.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
I solved the cout problem myself....but there are still more problems...
  #include <iostream>  using namespace std;  class big{  private:   static int hey=7;   static int you=10;   static int z;   public:   void main() {  cout<<mult(hey,you);  }  int mult(int x, int y) {  return x*y;  }};


The compiler complains that I have to make "hey" and "you" constants....I don't wanna do that! And even if I do make them into constants...it shows no syntax errors but the link error says that I have an undefined reference to 'winmain@16'...I'm confused!

Is it me or my compiler which has an undesired reference to ANSI c++.
Original post by ReneGade RG dev
   static int hey=7;   static int you=10;   static int z;

You simply cannot assign variables in a class (even static ones) simply at declaration time. As fruny clearly stated, you have to make a constructor to initialize non-static variables when an instance of big is created. For static variables, fruny also says clearly that you have to define them outside the class declaration (if your class is in a header, you might have to define them in one .cpp file) like that :
   static int hey;   static int you;   static int z;   ... };   //outside the class declaration   int big::hey = 7;   int big::you = 10;


This will surely work better now.
The compiler complains that I have to make "hey" and "you" constants....

Only static integral constants can be initialized the way you did - in the class definition. If you do not make them constants, you need to define the variable separately (in the class definition, it is only a variable declaration), as I have show you in my previous posts.

it shows no syntax errors but the link error says that I have an undefined reference to 'winmain@16'

It means you aren't providing the program's main function which, unlike in Java, isn't a static member of some class, but a non-member function.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement