Linker Error

Hi this is probably a really simple mistake but I haven't programmed much in C++ and am self taught so any help is appreciated.

I'm exploring the singleton pattern and this is my simple class that has the following error:
[Linker error] undefined reference to `First::singleRef'

class First
  int value;
  static First *singleRef;
    static First* getInstance();


  First* First::getInstance()
      singleRef = new First;
     return singleRef;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you add a static member to a class, you need to somewhere allocate space for that. In one of your implementation files, add

First *First::singleRef;
Ooops, better

First *First::singleRef = NULL;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
compiled fine with g++ but with a warning


here the second line  

"singleRef==0"   doesn't really do anything ... find out why :)[ hint == is not assignment]
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

twobitadderAuthor Commented:
Thanks for your comments, I'm attempting to allocate the space for the static member in the protected constructor that's called by the getInstance() function so doesn't

 singleRef = new First;

allocate the necessary space? I changed it also to
 First::singleRef = new First;

Also I didn't spot that i tested for equality rather than assigned, thanks for pointing that out.

I'm still getting the error though that says there's an undefined reference to First::singleRef
That does not matter, it is about the storage space for the pointer itself, not the allocated area it points to. As I wrote, add

First *First::singleRef = NULL;

to the implementation file.

try adding "inline" keyword to the function definition.

inline First* First::getInstance()


I feel (others have also pointed out in the previous comments) that there are a couple of
problems in your code.
First is that inside the constructor the statement 'singleRef==0' should be using the assignment '=' operator and not the comparison operator '==' as I presume you are trying
to initialize the member singleRef to NULL.
Second error is that the statement should not be inside the constructor!

It is like this: Inside your getInstance() function, you are trying to create an object of your class and assign its pointer to singleRef. But inside the constructor you are making singleRef 0!
Thus in the same line, you are trying to assign the pointer to a memory block and also trying to make it NULL. This is not a good practice.

You should do what someone has suggested in some previous comment i.e. in your source file, outside any function, you should have the following statement:

First* First::singleRef = NULL;

And your constructor should become:
First() {value=10;}

This should solve your problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.