473,486 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Compilation with Templates

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
3 1547

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1496
by: Connell Gauld | last post by:
Hi, I am having problems compiling a small project in VC++ 6. I have included the error below and the full project (including workspace) can be downloaded here:...
11
416
by: Alex Vinokur | last post by:
Hi, The code below has no problem with GNU g++ 3.3, but it has a problem with GNU g++ 3.4. What is reason for that? --------- foo.cpp : BEGIN --------- template <typename T> struct Boo
12
12832
by: blueblueblue2005 | last post by:
Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help. here is the files:...
4
1363
by: Marc.Tajchman | last post by:
Hi everybody, Here is a simple C++ example : 1 #include <vector> 2 3 template <typename T> 4 class X { 5 T _t; 6 public :
5
5840
by: Mikael S. H. | last post by:
Header file compilation I'm coding a small irc bot, and I've noticed that compilation takes very long when I add certain header files (compared to compilation time without). I've tried to find...
4
2717
by: Marcelo | last post by:
Hi everybody, This is my first time with the template class and I have an strange problem. I have spent all the afternoon trying to understand it, but I don't get the problem... I have three...
8
1762
by: vitalyt | last post by:
Hi, I have cpp file which consist nested templates. Compilation time without optimization is 1-2 minutes, with -O2 turned on more then 5 hours. :( Could anybody help with that problem? AIX 5.2,...
6
1438
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
1
2643
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.