?
Solved

Linker Error

Posted on 2004-11-07
7
Medium Priority
?
260 Views
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;
   
  public:
    static First* getInstance();

  protected:
    First(){value=10;singleRef==0;}
};

  First* First::getInstance()
  {
    if(First::singleRef==0)
      singleRef = new First;
     
     return singleRef;
  }
0
Comment
Question by:twobitadder
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12520378
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;
0
 
LVL 86

Accepted Solution

by:
jkr earned 260 total points
ID: 12520380
Ooops, better

First *First::singleRef = NULL;
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 120 total points
ID: 12520381
compiled fine with g++ but with a warning

 First(){value=10;singleRef==0;}

here the second line  

"singleRef==0"   doesn't really do anything ... find out why :)[ hint == is not assignment]
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Author Comment

by:twobitadder
ID: 12520403
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 12520448
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.
0
 
LVL 1

Expert Comment

by:ppk1981
ID: 12521502
hi,

try adding "inline" keyword to the function definition.
ex:

inline First* First::getInstance()
{

}

cheers
praveen
0
 

Assisted Solution

by:AniOnLine
AniOnLine earned 120 total points
ID: 12532304
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.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

864 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