Page 1 of 1

Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 3:54 pm
by Bullet Pulse
What I have is:

Code: Select all

//x.h
class X
{
public:
   static void Initialize(X *obj1, X *obj2);
private:
   static X *x1;
   static X *x2;
};

//x.cpp
#include "x.h"
X *X::x1;
X *X::x2;

void X::Initialize(X *obj1, X *obj2)
{
   x1 = obj1;
   x2 = obj2;
}
I get the following error in VC++:
1>..\x.cpp(19) : error C2065: 'x1' : undeclared identifier
1>..\x.cpp(19) : error C2065: 'x2' : undeclared identifier


EDIT:
I fixed it by putting the Initialize() function definition in the header file.
Does anyone know why this works?

Re: Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 4:09 pm
by GroundUpEngine
Shouldn't it be ->

Code: Select all

void X::Initialize(X *obj1, X *obj2)
{
   x1 = obj1;
   x2 = obj2;
}

Re: Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 4:11 pm
by Falco Girgis
Bullet Pulse wrote:What I have is:

Code: Select all

//x.h
class X
{
public:
   static void Initialize(X *obj1, X *obj2);
private:
   static X *x1;
   static X *x2;
};

//x.cpp
#include "x.h"
X *X::x1;
X *X::x2;

void Initialize(X *obj1, X *obj2)
{
   x1 = obj1;
   x2 = obj2;
}
I get the following error in VC++:
1>..\x.cpp(19) : error C2065: 'x1' : undeclared identifier
1>..\x.cpp(19) : error C2065: 'x2' : undeclared identifier


EDIT:
I fixed it by putting the Initialize() function definition in the header file.
Does anyone know why this works?
Yeah, you're not defining the function correctly in the C++ file.

You forgot the scope resolution operator on the class name. Change:

Code: Select all

void Initialize(X*, X*)
to

Code: Select all

void X::Initialize(X*, X*)

Re: Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 4:12 pm
by Falco Girgis
GroundUpEngine wrote:Shouldn't it be ->

Code: Select all

void X::Initialize(X *obj1, X *obj2)
{
   x1 = obj1;
   x2 = obj2;
}
YOU BASTARD!

Re: Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 4:13 pm
by GroundUpEngine
GyroVorbis wrote:
GroundUpEngine wrote:Shouldn't it be ->

Code: Select all

void X::Initialize(X *obj1, X *obj2)
{
   x1 = obj1;
   x2 = obj2;
}
YOU BASTARD!
Image

Re: Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 4:26 pm
by Bullet Pulse
I actually meant to include that in the example, so that is not part of the error.

Re: Class X carrying a pointer to an object of class X

Posted: Sat May 15, 2010 9:13 pm
by RyanPridgeon
Bullet Pulse wrote:What I have is:

Code: Select all

X *X::x1;
X *X::x2;
[/quote]

Yeah.... don't do that.

[b]Edit:[/b]
You only have to declare the variables in a class once; it's the [i]functions[/i] that can be defined outside the class definition and declared inside.
So Just remove those two lines.

Re: Class X carrying a pointer to an object of class X

Posted: Sun May 16, 2010 10:29 am
by Ginto8
RyanPridgeon wrote:
Bullet Pulse wrote:What I have is:

Code: Select all

X *X::x1;
X *X::x2;
Yeah.... don't do that.

Edit:
You only have to declare the variables in a class once; it's the functions that can be defined outside the class definition and declared inside.
So Just remove those two lines.
They're static, therefore they have to be declared in a source file to be allocated... one of the annoying things about the header file method. :(

Anyway, I'm not sure, but there may be an issue with the fact that the declaration of the type X hasn't completed, so the compiler doesn't know what type x1 and x2 are really supposed to be. Try putting:

Code: Select all

class X;
before your class declaration, and see if that helps.