By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,605 Members | 2,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,605 IT Pros & Developers. It's quick & easy.

Compilation with Templates

P: n/a
Hi,

I have the following problem. Consider this:

// codec.h
template <class T>
class codec {
public:
codec(T val);
private:
T val;
};

// codec.cpp
#include "codec.h"
template <class T>
codec<T>::codec(T val) { this->val = val; }

// main.cpp
#include "codec.h"
void main() { codec<int> cc(7); }

I compile using: c++ codec.cpp main.cpp
and I get:
/tmp/ccwlkOaV.o: In function `main':
/tmp/ccwlkOaV.o(.text+0x10): undefined reference to `codec<int>::codec(int)'
collect2: ld returned 1 exit status

But if I either move the contents of codec.cpp to the codec.h file OR
move the main to codec.cpp, everything works out. Why ?

Is there a way to make this work having the three files that I have ?

Thanks -
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Ajay Daptardar" <aj**@brandeis.edu> wrote in message news:pa**********************************@brandeis .edu...

But if I either move the contents of codec.cpp to the codec.h file OR
move the main to codec.cpp, everything works out. Why ?

Is there a way to make this work having the three files that I have ?

Most of the compilers out there can't handle separate compilation of templates.
Even if they did, you'd need to explicitly declare it as an exported template
(which you don't do). .

-Ron

main must return int, by the way.

Jul 22 '05 #2

P: n/a
Ajay Daptardar wrote in
news:pa**********************************@brandeis .edu:
Hi,

I have the following problem. Consider this:

// codec.h
template <class T>
class codec {
public:
codec(T val);
private:
T val;
};

// codec.cpp
#include "codec.h"
template <class T>
codec<T>::codec(T val) { this->val = val; }

// main.cpp
#include "codec.h"
void main() { codec<int> cc(7); }

I compile using: c++ codec.cpp main.cpp
and I get:
/tmp/ccwlkOaV.o: In function `main':
/tmp/ccwlkOaV.o(.text+0x10): undefined reference to
`codec<int>::codec(int)' collect2: ld returned 1 exit status

But if I either move the contents of codec.cpp to the codec.h file OR
move the main to codec.cpp, everything works out. Why ?

Is there a way to make this work having the three files that I have ?


You need to get a compiler that supports export (comaeu is currently
the only one) and readup on how to use it, or put the defenition of
codec<T>::codec(T val) in the header (.h) file.

templates aren't instantiated until you actually use them, and then
only if the compiler has seen a defenition.

In your example the compiler instantiates codec<T>::codec(T val),
with T = int, in main(), but only if its seen it already. This
doesn't happen if you have the defenition in another translation
unit (.cpp file), hence the linker failes to find it and you
get the error.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a
Ajay Daptardar wrote:


I have the following problem. Consider this:

// codec.h
template <class T>
class codec {
public:
codec(T val);
private:
T val;
};

// codec.cpp
#include "codec.h"
template <class T>
codec<T>::codec(T val) { this->val = val; }
template class codec<int>;

// main.cpp
#include "codec.h"
int main(int argc, char* argv[]) { codec<int> cc(7); return 0; }

I compile using: c++ codec.cpp main.cpp and I get:

/tmp/ccwlkOaV.o: In function `main':
/tmp/ccwlkOaV.o(.text+0x10): undefined reference to `codec<int>::codec(int)'
collect2: ld returned 1 exit status But if I either move the contents of codec.cpp to the codec.h file OR
move the main to codec.cpp, everything works out. Why?
Because the entire class template definition
including template <class T> codec<T>::codec(T val)
was available to your compiler when it instantiated codec<int>.
Is there a way to make this work having the three files that I have?


You should instantiate template class codec<int> *explicitly*
in codec.cpp

Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.