//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.
//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:
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
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: