473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 1540

"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
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
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
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
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
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
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
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
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
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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.