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
  • 14,503
  • 22
  • 92
  • 150

2 Answers2

4

You forgot to add

  MyComp* Logger::pComp;

to your cpp file (outside the class declation).

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
0

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

elnineo
  • 17
  • 1
  • 6