Singleton compilation error LNK2001

I'm trying Singleton to generate unique number during a session and arrived at the error

Error 1 error LNK2001: unresolved external symbol "private: static class IDGenerator * IDGenerator::only_copy" (?only_copy@IDGenerator@@0PAV1@A) C:\Users\Bonaventure\Documents\Visual Studio 2013\Projects\Test\Test\Test.obj Test


Within "EventDef.h"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef EVENT_DEFINTIONS_H
#define EVENT_DEFINTIONS_H

class IDGenerator {
private:
	IDGenerator() : _id(0) {}

	static IDGenerator* only_copy;
	long _id;
public:
	   static IDGenerator* instance() {
		   if (!only_copy) {
			   only_copy = new IDGenerator();
		   }
		   return only_copy;
	   }
	   long next() { return _id++; }
};

#endif 


Test.cpp
1
2
3
4
5
#include "EventDef.h"

void main() {
  long num = IDGenerator::instance()->next();
}



Because it is a static instance, you need to initialize it in another CPP file as well.

Also, why do you need this just to generate unique ID's? This is all you need:
1
2
3
4
unsigned long IDGenerator() {
    static unsigned long id = 0;
    return id++;
}
Last edited on
oh great that's even better thanks.
Topic archived. No new replies allowed.