Help with undefined references

S

sieg1974

Hi,

I know it must be a simple mistake, but I'm just learning C++ and can't
figure out what's wrong. Any help will be vey much appreciated :)
I got the following error messages when the following program is
compiled and linked. What could be wrong?

Thanks,

Andre

/tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
/tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
/tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
collect2: ld returned 1 exit status

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();
~Singleton();

private:
Singleton();
static Singleton * theSingleton;
};

Singleton::Singleton()
{
std::cout << "Singleton constructot\n";
}

Singleton::~Singleton()
{
std::cout << "Singleton destructor\n";
}

Singleton * Singleton::Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
}

Singleton * theSingleton = 0;

int main()
{
return( 0 );
}
 
G

Greg

Hi,

I know it must be a simple mistake, but I'm just learning C++ and can't
figure out what's wrong. Any help will be vey much appreciated :)
I got the following error messages when the following program is
compiled and linked. What could be wrong?

Thanks,

Andre

/tmp/ccX0Z0Il.o(.text+0x17d): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
/tmp/ccX0Z0Il.o(.text+0x1ca): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
/tmp/ccX0Z0Il.o(.text+0x1cf): In function `Singleton::Instance()':
: undefined reference to `Singleton::theSingleton'
collect2: ld returned 1 exit status

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();
~Singleton();

private:
Singleton();
static Singleton * theSingleton;
};
Singleton * theSingleton = 0;

Should be:

Singleton * Singleton::theSingleton = 0;

The member name needs to be qualified with its class name:

Greg
 
S

sieg1974

Thanks for the help, I could make it work now. But I have another
question :).
I have the following 2 versions of my program. The first one works
fine, but not the second one. I get the same linking erros as before.
Can anyone point out what could be wrong?

Thanks,

Andre

***********
Version 1
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
}

private:
static Singleton * theSingleton;

};

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
}

***********
Version 2
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();

private:
static Singleton * theSingleton;

};

Singleton * Singleton::Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
}

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
}
 
V

Victor Bazarov

Thanks for the help, I could make it work now. But I have another
question :).
I have the following 2 versions of my program. The first one works
fine, but not the second one. I get the same linking erros as before.

Same, which?
Can anyone point out what could be wrong?

Thanks,

Andre

***********
Version 1
***********

#include <iostream>
^^^^^^^^^^^^^^^^^^^
You don't need this since you use nothing from that header.
class Singleton
{
public:
static Singleton * Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );

I really wish you'd drop the parentheses around the expression in the
'return' statement. Makes your code look amateurish. 'return' is not
a function. There is no need for parentheses!
}

private:
static Singleton * theSingleton;

};

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
Ugh!

}

***********
Version 2
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();

private:
static Singleton * theSingleton;

};

Singleton * Singleton::Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();
}

return( theSingleton );
Yuck!

}

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
Yechh!

}

I don't get any link errors if I try building your programs from
the source code you posted.

V
 
S

Salt_Peter

Thanks for the help, I could make it work now. But I have another
question :).
I have the following 2 versions of my program. The first one works
fine, but not the second one. I get the same linking erros as before.
Can anyone point out what could be wrong?

Thanks,

Andre

***********
Version 1
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();

try:
theSingleton = new Singleton;
}

return( theSingleton );

return theSingleton;
}

private:
static Singleton * theSingleton;

};

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );
}

***********
Version 2
***********

#include <iostream>

class Singleton
{
public:
static Singleton * Instance();

private:
static Singleton * theSingleton;

};

Singleton * Singleton::Instance()
{
if( !theSingleton )
{
theSingleton = new Singleton();

same changes...
theSingleton = new Singleton;
}

return( theSingleton );
}

Singleton * Singleton::theSingleton = 0;

int main()
{
return( 0 );

return 0;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top