Jump to content
  • Advertisement
Sign in to follow this  
ReneGade RG dev

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

This topic is 4849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
If you want one variable per instance of the class.

Foo.h
class 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.h
class Foo
{
static int x;
};

Foo.cpp
int Foo::x = 10; // x is set to 10 at program startup

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
If you want one variable per instance of the class.

Foo.h
class 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.h
class Foo
{
static int x;
};


Foo.cpp
int 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"...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!