473,545 Members | 524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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 4889
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<typena me T> class X{
void func(const T&);
}
#include "foo.cpp"

// foo.cpp
template<typena me T> void X<T>::func(cons t T&){}

Jul 22 '05 #2
Dear Jacques,

Thanks!

--
LaBird (Benny).

"Jacques Labuschagne" <ja*****@clawsh rimp.com> wrote in message
news:xH******** ************@ne ws02.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<typena me T> class X{
void func(const T&);
}
#include "foo.cpp"

// foo.cpp
template<typena me T> void X<T>::func(cons t T&){}

Jul 22 '05 #3

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

Similar topics

3
7753
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 what I believe are linker errors. For example: g++ -g -O2 -o rematrix cslice_iter.o main.o matrix.o rematrix.o slice_iter.o...
8
4565
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
6
3300
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 uses the functions of htable.cpp. I used the command: "g++ -Wall ir3tst.cpp -o bin/Linux/ir3tst -Llib/Linux -lbasics" and get some errors...
9
4830
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 file which is still named as .h file, it compiled and run. here are files: // Listnode.h #ifndef LISTNODE_H #define LISTNODE_H
2
75775
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 through this code multiple times and have no idea what would cause an error like this, I've never seen one like it before. Any help would be greatly...
8
2533
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 C2079: 'first' uses undefined class 'Node' f:\xxxxx\product\rw\rcb1.2.0\rm\include\vector(404) : see reference to class template instantiation...
1
3829
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 behavior is undefined." In the program below, what constitutes "point of deletion"? Presumably, since delete is called somewhere within shared_ptr's...
3
2039
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
1801
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 chaper 11th, they teach us how to implement our own vector. I rewrite the program, but I found a very curious question. When you place the Vec<T>::Vec( )...
0
7455
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7390
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7410
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5959
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3441
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3437
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1867
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1010
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
692
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.