Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
X is a base class in which 'data' is being set. while executing the code I am getting this error: In function `main':
undefined reference to `X::data;

Can anyone please help me resolve this error.
Any help will be appreciated.



C++
#include <iostream>
using namespace std;
struct ICP{ int id; };
struct Q752{ int id; };
class X
{
protected:
enum{ DT =0, Q7};
static void *data;
public:
  X();
  ~X();
  static void *get() { return data; }
   static void set(int type) 
   {
     cout << "X::set()" <<endl;
     
     switch(type)
     {
       case DT:
               {
                 cout << "case: ICP" <<endl;
                 ICP* dt= new ICP;
                  //cout << (int)dt << endl;
                 dt->id = 10; data = dt;
               }
               break;
       case Q7:
               {
                 
                 cout << "case: Q752" <<endl;
                 Q752* dt1 = new Q752;
                 dt1->id = 20;// data = (Q752*)dt1;
               } 
               break;
       default: 
               cout << "Invlaid type";
     }
   }
  static void delX(int type) { if(data)
                        switch(type){
                          case DT:
                              delete (ICP*)data;
                          case Q7:
                              delete (Q752*)data;
                          default:
                               cout<< "DEL ERROR" << endl;
                        } 
                     }

};

class A{
friend class X;
public:
A();
~A() { X::delX(0); }
static void set(int type) {X::set(type);}
static ICP *get() { return (ICP *)X::get();}
};

class B{
friend class X;
public:
B();
~B(){ X::delX(1); }
static void set(int type) {X::set(type);}
static Q752*get() { return (Q752*)X::get();}
};

int main()
{
cout << "Set() is Start!!" <<endl;
A::set(0);
cout << "Set() is Done!!" <<endl;

return 0;
}
Posted

Hi,

Every static variable must be initialized on global level. That is what is missing in your code.

Best regrads,
J. K.
 
Share this answer
 
v2
Comments
Member 8576081 21-Feb-13 5:56am    
Thanks for your help!
You have not created an instance of class X so X::data does not exist anywhere, even though it is defined in the class. Also why have you made everything in your classes static?
 
Share this answer
 
Comments
Member 8576081 21-Feb-13 5:57am    
Thank you for your time! I really appreciate your help!
Static members have to be initialized exactly once; Outside the declaration of your X class; add this;
C++
void* X::data = 0; // Or whatever value you want to initialize this pointer to


Hope this helps,
Fredrik
 
Share this answer
 
Comments
Member 8576081 21-Feb-13 5:57am    
Thank you for such a prompt answer!
Fredrik Bornander 21-Feb-13 5:58am    
Glad I could help.
H.Brydon 21-Feb-13 12:09pm    
I'm not arguing with your solution but actually, you can initialize some static members inside the class declaration (ie. 'int' and variations, and (I think) pointers, not float/double).
Fredrik Bornander 21-Feb-13 13:47pm    
I thought implicit initialization only worked for const static members?
H.Brydon 21-Feb-13 14:05pm    
Ack you are correct, it has to be 'const':

class Blah
{
public:
Blah(){}

static int m_x1 = 0; // error
static const int m_x2 = 0; // ok
static int* m_px3 = NULL; // error
static const int* m_px4 = NULL; // error
static int* const m_px5 = NULL; // error
static const int* const m_px6 = NULL; // error
static const float m_xf1 = 0.0f; // error (already knew that)
};

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900