gcc doesn't compile templates

M

Michael Schutte

I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

template <typename T>
Wrapper<T>::Wrapper(T avalue) {
value = avalue;
}
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o
/tmp/abcdefgh.o(...): In function `main':
undefined reference to Wrapper<int>::Wrapper[in-charge](T)
collect2: ld returned 1 status
What have I done wrong?
 
R

Rob Williscroft

Michael Schutte wrote in @newsreader02.highway.telekom.at in comp.lang.c++:
I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

Move all the (template) code into your header file
template <typename T>
Wrapper<T>::Wrapper(T avalue) {
value = avalue;
}

You can omit this (if wrapper.cpp is now empty)
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o

Assuming wrapper.cpp is now empty:

$ gcc -o testwrapper testwrapper.cpp


Read the FAQ:

http://www.parashift.com/c++-faq-lite/

It will take a while, but it will be worth it.

HTH.

Rob.
 
J

John Harrison

Michael Schutte said:
I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

template <typename T>
Wrapper<T>::Wrapper(T avalue) {
value = avalue;
}
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o
/tmp/abcdefgh.o(...): In function `main':
undefined reference to Wrapper<int>::Wrapper[in-charge](T)
collect2: ld returned 1 status
What have I done wrong?

Put the template in the header file. Templates belong in header files unless
your compiler supports the export keyword (very few do).

We must get this question about three times a week. It's in the FAQ, its a
good idea to check the FAQ before posting

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.14

john
 
M

Michael Schutte

I'm sorry that I haven't checked the FAQs -- I know about the
netiquette. But I mustn't stay long in the WWW (I'm 13 years old and
must use the account of my parents) and have already searched for a long
time...
 
A

Aggro

Michael said:
I'm sorry that I haven't checked the FAQs -- I know about the
netiquette. But I mustn't stay long in the WWW (I'm 13 years old and
must use the account of my parents) and have already searched for a long
time...

Download the faq to your local computer and search off line.
 
R

Rakesh Kumar

Why don't u try using 'g++', instead of gcc.

Michael said:
I am trying to write a wrapper class, which can store any data type to
wrap it into a class. My compiler is gcc (for c++) version 3.3.1 on a
SuSE Linux 9.0 system.
I start with the following code:
template <typename T>
class Wrapper {
public:
Wrapper(T avalue);
private:
int value;
};
in the header wrapper.h, and in wrapper.cpp:
#include "wrapper.h"

template <typename T>
Wrapper<T>::Wrapper(T avalue) {
value = avalue;
}
When I compile now, using
$ gcc -c wrapper.cpp
gcc doesn't report any errors.
Linking with the resulting wrapper.o:
$ gcc -o testwrapper testwrapper.cpp wrapper.o
/tmp/abcdefgh.o(...): In function `main':
undefined reference to Wrapper<int>::Wrapper[in-charge](T)
collect2: ld returned 1 status
What have I done wrong?
 

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

Forum statistics

Threads
474,166
Messages
2,570,907
Members
47,446
Latest member
Pycoder

Latest Threads

Top