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;
}
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;
}