473,396 Members | 1,996 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,396 software developers and data experts.

Undefined reference on template class

Dear all,

I would like to ask what is the way to separate class
template definition from declaration in different source
files? Currently I have the following files (simplified):

// d.h -- class declaration
template<typename T>
class C {
int x;
T* p;
public:
C();
T* getp();
};
// d.cpp -- class definition
#include "d.h"

template <typename T>
C<T>::C() {
x = 0;
p = new T;
}

template <typename T>
T* C<T>::getp() {
return p;
}
// my.cpp -- using class C
#include "d.h"

main() {
C<int> c;
int *q;

q = c.getp();
return 0;
}

But when I compile the two .cpp files, I get an error
from the loader, about undefined reference on the
c.getp() and constructor. I found that this error
will not occur if I do not have the template. Why does
such error occurs for templates, and how to solve it
(apart from putting the member function definitions
into d.h)?

Thanks a lot.

--
LaBird (Benny).
Jul 22 '05 #1
2 4880
LaBird wrote:
Dear all,

I would like to ask what is the way to separate class
template definition from declaration in different source
files?


No. (Your compiler doesn't support the "export" keyword.)

The nearest hack-around is

// foo.hpp
template<typename T> class X{
void func(const T&);
}
#include "foo.cpp"

// foo.cpp
template<typename T> void X<T>::func(const T&){}

Jul 22 '05 #2
Dear Jacques,

Thanks!

--
LaBird (Benny).

"Jacques Labuschagne" <ja*****@clawshrimp.com> wrote in message
news:xH********************@news02.tsnz.net...
LaBird wrote:
Dear all,

I would like to ask what is the way to separate class
template definition from declaration in different source
files?


No. (Your compiler doesn't support the "export" keyword.)

The nearest hack-around is

// foo.hpp
template<typename T> class X{
void func(const T&);
}
#include "foo.cpp"

// foo.cpp
template<typename T> void X<T>::func(const T&){}

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Steven T. Hatton | last post by:
Scroll to the bottom and read the last part first. I've been trying very diligently to 'modularize' the code from TC++PL3E found here: http://www.research.att.com/~bs/matrix.c I keep getting...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
6
by: Christian Christmann | last post by:
Hi, I've created a file htable.cpp which I compiled to htable.o and than added with "ar" to a library libbasics.a in the directory lib/Linux/. Now I want to compile another file ir3tst.cpp which...
9
by: blueblueblue2005 | last post by:
Here is a very simple template example, whenever I seperate the header and implementation file, I got "undefined reference to member function error. and if I put the header and definition in one...
2
by: B_Love | last post by:
Hey! When trying to compile the code for a ordered vector class I get the following error: undefined reference to `WinMain@16' Anyone have any idea what I might be doing wrong? I've been...
8
by: akira2x3x | last post by:
Hello, I get this error while compiling with visualc++ and STL roguewave. With STL microsoft everything work fine. XXXData.cpp f:\xxxxx\product\rw\rcb1.2.0\rm\include\rw\_pair.h(63) : error...
1
by: Alan Johnson | last post by:
From the standard 5.3.5/5: "If the object being deleted has incomplete class type at the point of deletion and the complete class has a non-trivial destructor or a deallocation function, the...
3
by: s.z.s | last post by:
Hi! I hope the solution to that is not too stupid... I've got three files: <snip test_main.cc> #include"test.hh" int main(void) { A<inta1; a1.saywhat();
4
by: Hora | last post by:
Hi Guys I'm reading Andrew Koenig and Barbara E. Moo 's book, Accelerated C++. It's the best works for newbie I ever read. The way the authors introduce the language is very interesting. In...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.