Click here to Skip to main content
13,140,231 members (49,532 online)
Rate this:
 
Please Sign up or sign in to 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.



#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 20-Feb-13 23:12pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Static members have to be initialized exactly once; Outside the declaration of your X class; add this;
void* X::data = 0; // Or whatever value you want to initialize this pointer to


Hope this helps,
Fredrik
  Permalink  
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)
};
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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?
  Permalink  
Comments
Member 8576081 21-Feb-13 5:57am
   
Thank you for your time! I really appreciate your help!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hi,

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

Best regrads,
J. K.
  Permalink  
v2
Comments
Member 8576081 21-Feb-13 5:56am
   
Thanks for your help!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.170915.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100