undefined template reference again ??

B

blueblueblue2005

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

template< typename NODETYPE >
class ListNode
{
public:
ListNode( const NODETYPE & );
NODETYPE getData() const;
private:
NODETYPE data;
ListNode< NODETYPE > *nextPtr;
};

#endif



// Listnode.cpp
#include "Listnode.h"

// constructor
template< typename NODETYPE >
ListNode< NODETYPE >::ListNode( const NODETYPE &info )
: data( info ), nextPtr( 0 )
{
}
// return copy of data in node
template< typename NODETYPE >
NODETYPE ListNode< NODETYPE >::getData() const
{
return data;
}




// t.cpp -- testing file
#include <iostream>
#include "Listnode.h"

using namespace std;

int main() {
ListNode<int> n(89);
cout << n.getData() << endl;
return 0;
}
 
Z

Zenith

How do u compile these files? I guess you might key in something like,
g++ t.cpp
That is sure to fail if you seperate Listnode.h and its implementation
cpp file, as from t.cpp only you cannot reach the implementation.
You may first compile a listnode.o from listnode.h and listnode.cpp,
and then compile t.cpp with the object file. A simple makefile should
work fine.
 
B

blueblueblue2005

It seems for class template, the header and implementation must be in
one file. I just checkedt the Deital book, all its template examples
are in this way.
 
T

Tobias Blomkvist

blueblueblue2005 said:
It seems for class template, the header and implementation must be in
one file. I just checkedt the Deital book, all its template examples
are in this way.

Or, at the end of the header-file you explicitly include the cpp-file,
and wrap the definitions within a macro-define scoped around the include.

#<template.h>--------------------------

template<typename T>
class test {
public:
void f();
};

#define INCLUDE_SHEILD
#include "template.cpp"
#undef INCLUDE_SHEILD

#--------------------------------------

#<template.cpp>------------------------

#ifdef INCLUDE_SHEILD

template<typename T>
void test<T>::f() {}

#endif

#--------------------------------------

Tobias
 
T

Tobias Blomkvist

blueblueblue2005 sade:
well, neither way works :(

Well, the export keyword is badly supported by compilers as
I understand it, but the other given solution works perfectly
for me and my borland environment, I often use it to split
the template declarations and definitions into different units.

Tobias
 
B

Ben Pope

Tobias said:
Or, at the end of the header-file you explicitly include the cpp-file,
and wrap the definitions within a macro-define scoped around the include.

....or explicitly instantiate the template(s) you require, at the bottom of the cpp-file.

Ben
 
Z

Zorro

I am replying to your attempt of separating template defs from the
bodies of methods. The best way to think about this (within the context
of C++) is as follows.

At the point of instantiation, for instance,

my_class<some_type> object;

the compiler needs access to the bodies so it can generate code. That
is why the bodies are included in header file.

There are other complications that can result in linkage errors. But
this will help you with the problem you are posting.

Hope it helps.
Z.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top