LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2011, 02:18 AM   #1
harishisnow
LQ Newbie
 
Registered: Nov 2010
Location: bangalore, india
Posts: 17

Rep: Reputation: 0
Linker error for a singleton class program


Hello,

I am getting a linker error for a simple program to represent a singleton class.

Please find below the program which I have written:

#include <iostream>

using namespace std;
class Singleton
{
private:
Singleton(); // making constructor private
Singleton(const Singleton &); // making copy constructor const..prevent copy construction
Singleton& operator=(const Singleton &); // making overloaded assignment operator const..prevent assignment
static Singleton *ptr;
public:
static Singleton* get_instance()
{
if (ptr == NULL)
{
ptr = new Singleton;
}
return ptr;
}
};

Singleton *Singleton:tr=NULL;

int main()
{
cout << "hello :\n";

Singleton* pobj = Singleton::get_instance();
return 0;
}


I am getting the below error when I am compiling:
CC singleton.cpp
Undefined first referenced
symbol in file
Singleton::Singleton() singleton.o
ld: fatal: Symbol referencing errors. No output written to a.out


Please help me out of this problem.

Regards,
Harish
 
Old 05-05-2011, 03:47 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
I've never seen error messages like that before - just out of interest, what compiler are you using?

Your error is because you've told the compiler you'll provide a Singleton::Singleton(void) method because you declared it, but you haven't defined it.
 
Old 05-06-2011, 12:43 AM   #3
harishisnow
LQ Newbie
 
Registered: Nov 2010
Location: bangalore, india
Posts: 17

Original Poster
Rep: Reputation: 0
Hello,
I am compiling the program on a solaris machine using CC compiler.

Regards,
Harish
 
Old 05-06-2011, 12:45 AM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
As JohnGraham's post suggests, presumably you need to also pass the file that contains the implementation of the Singleton class' methods.

Edit: you also should post code inside [code] tags, to preserve indentation and therefore aid readability.

Last edited by Nylex; 05-06-2011 at 12:49 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Compilation error trying to sub-class a Qt QAction class, not sure why rstewart Programming 2 02-08-2011 11:45 AM
linker script file not found while using codesourcery g++ lite to program a developme raphtor Linux - Software 0 10-21-2009 04:04 AM
[SOLVED] cryptic linker error (i really dont like linker errors); smeezekitty Programming 2 09-19-2009 02:21 AM
C++ Trying singleton behavior, keep getting 'undefined reference to Class::instance() x_terminat_or_3 Programming 10 05-13-2008 02:01 PM
C++ - class template - linker errors? Wynd Programming 2 10-19-2004 03:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration