Re: Undefined reference to '...'
Are you linking with gcc or g++? Try g++ for the link phase.
Here is the output I get for a simple test program:
------ Begin temp.C --------
#include <iostream.h>
template<class T>
class ancestor {
private:
T myvar;
public:
ancestor(T x) { cout << "ancestor()" << endl; myvar = x; }
~ancestor() { cout << "~ancestor()" << endl; }
void dump() { cout << "ancestor.myvar: " << myvar << endl; }
};
int
main( int argc, char* argv ) {
ancestor<int> something(5);
something.dump();
return 0;
}
------ End temp.C --------
> g++ temp.C -o temp
> temp
ancestor()
ancestor.myvar: 5
~ancestor()
> gcc temp.C -o temp
/tmp/cca021531.o: In function `ancestor<int>::~ancestor(void)':
/tmp/cca021531.o(.text+0x6d): undefined reference to `endl(ostream &)'
/tmp/cca021531.o(.text+0x77): undefined reference to `cout'
/tmp/cca021531.o(.text+0x7c): undefined reference to
`ostream::operator<<(char const *)'
/tmp/cca021531.o(.text+0x87): undefined reference to
`ostream::operator<<(ostream &(*)(ostream &))'
/tmp/cca021531.o: In function `ancestor<int>::dump(void)':
/tmp/cca021531.o(.text+0xca): undefined reference to `endl(ostream &)'
/tmp/cca021531.o(.text+0xd7): undefined reference to `cout'
/tmp/cca021531.o(.text+0xdc): undefined reference to
`ostream::operator<<(char const *)'
....
Bob
Orn E. Hansen wrote:
> I've encountered a condition in GNU C++, where I define the following
> template function...
>
> template<class T>
> class ancestor {
> private:
> ...
> public:
> ancestor()
> ~ancestor()
> };
>
> template<class T>
> ancestor<T>::ancestor()
> {
> ...
> }
>
> ...
>
> and use it in a main program...
>
> main()
> {
> ancestor<int> something;
>
> something...
> }
>
> The program compiles without errors, but when the linker is to link the
> object files, it is persistent that the reference to 'something' within
> the main program is undefined to 'ancestor<...>'. Every reference to
> the object 'ancestor<int>' is reported as undefined in the program.
>
> Does anyone have a clue as to why this occurs, and if there is a cure
> to this?
>
> --
> TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
> debian-user-request@lists.debian.org .
> Trouble? e-mail to templin@bucknell.edu .
--
-----------------
resendes@ici.net
--
TO UNSUBSCRIBE FROM THIS MAILING LIST: e-mail the word "unsubscribe" to
debian-user-request@lists.debian.org .
Trouble? e-mail to templin@bucknell.edu .
Reply to: