Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Undefined reference on template class

Reply
Thread Tools

Undefined reference on template class

 
 
LaBird
Guest
Posts: n/a
 
      01-09-2004
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).


 
Reply With Quote
 
 
 
 
Jacques Labuschagne
Guest
Posts: n/a
 
      01-09-2004
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&){}

 
Reply With Quote
 
 
 
 
LaBird
Guest
Posts: n/a
 
      01-09-2004
Dear Jacques,

Thanks!

--
LaBird (Benny).

"Jacques Labuschagne" <(E-Mail Removed)> wrote in message
newsHvLb.16494$(E-Mail Removed)...
> 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&){}
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
undefined template reference again ?? blueblueblue2005 C++ 9 07-24-2005 07:31 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments