Class X carrying a pointer to an object of class X

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Class X carrying a pointer to an object of class X

Post 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?
Last edited by Bullet Pulse on Sat May 15, 2010 4:27 pm, edited 1 time in total.
Image
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post by GroundUpEngine »

Shouldn't it be ->

Code: Select all

void X::Initialize(X *obj1, X *obj2)
{
   x1 = obj1;
   x2 = obj2;
}
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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*)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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!
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

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

Post 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
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

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

Post by Bullet Pulse »

I actually meant to include that in the example, so that is not part of the error.
Image
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

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

Post 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.
Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

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

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply