Linker Error

Posted on 2004-11-07
Last Modified: 2010-04-01
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;
Question by:twobitadder
    LVL 86

    Expert Comment

    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;
    LVL 86

    Accepted Solution

    Ooops, better

    First *First::singleRef = NULL;
    LVL 11

    Assisted Solution

    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]
    LVL 5

    Author Comment

    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
    LVL 86

    Expert Comment

    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.
    LVL 1

    Expert Comment


    try adding "inline" keyword to the function definition.

    inline First* First::getInstance()



    Assisted Solution

    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.

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Suggested Solutions

    What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now