1

I am getting linking error when I build the project. I have a static member pointer which I am setting from a static member function. Any ideas what's the problem

class Logger
{

 private:   
  static MyComp* pComp;
 public:    
  static void setComp(MyComp* comp);
      // more methods ..
};  

void Logger::setComp(MyComp* comp)

{   
pComp = comp; 
}

Get the linking error

Undefined symbols for architecture x86_64:
  "Logger::pComp", referenced from:
      Logger::setComp(MyComp*) in Logger.o

Ahmed
  • 13,816
  • 21
  • 89
  • 143

2 Answers2

4

You forgot to add

  MyComp* Logger::pComp;

to your cpp file (outside the class declation).

Doc Brown
  • 19,153
  • 6
  • 51
  • 87
0

In order to initialize a static data-member we must include a formal definition outside the class.

elnineo
  • 17
  • 1
  • 6