Linking problems making a singleton

Dec 29, 2011 at 3:30pm
Hi all,
I'm trying to re-learn c++ after being away for 4 or 5 years. I'm trying to create a singleton class, but when I attempt to link I get an error.

Here is the header (in file singleton.h):

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

class singleton
{
public:
	typedef singleton * singletonPtr;
private:
	static singletonPtr mySingleton;
	singleton();

public:
	singletonPtr getTheSingleton();
};


Here is the body (in file singleton.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
#include "singleton.h"

singleton::singleton()
{
}

singleton::singletonPtr singleton::getTheSingleton()
{
	if (!mySingleton)
		mySingleton = new singleton();
	return mySingleton;
}


And the linker message I get:
 
1>singleton.obj : error LNK2001: unresolved external symbol "private: static class singleton * singleton::mySingleton" (?mySingleton@singleton@@0PAV1@A)


The compiler is visual studio 2008, although I suspect the problem is me, not the compiler. Any suggestions what I'm doing wrong and how to fix it?

Thanks
James Alan Farrell
Dec 29, 2011 at 3:45pm
Never mind -- found it on the internet.
Always search before making a post, but never find it until after I make the post ;)

I've been doing Java for the past few years, and it's hard to remember these things!
Dec 30, 2011 at 1:40am
Aren't you supposed to return it as a reference or pointer? Returning it as a copy kinda defeats the whole purpose, no?

Edit, nevermind, you typedefed it. You only need one class scope operator and it goes inbetween the return variable or void and the Method name. Good luck!
Last edited on Dec 30, 2011 at 1:44am
Topic archived. No new replies allowed.