By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,920 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,920 IT Pros & Developers. It's quick & easy.

undefined template reference again ??

P: n/a
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;
}

Jul 24 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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.

Jul 24 '05 #2

P: n/a
I compiled all the .cpp file

g++ t.cpp Listnode.cpp

Jul 24 '05 #3

P: n/a
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.

Jul 24 '05 #4

P: n/a
blueblueblue2005 wrote:
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.


Lookup keyword export

http://www.kuzbass.ru:8086/docs/isocpp/template.html

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 24 '05 #5

P: n/a
blueblueblue2005 wrote:
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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 24 '05 #6

P: n/a
well, neither way works :(

Jul 24 '05 #7

P: n/a
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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 24 '05 #8

P: n/a
Tobias Blomkvist wrote:
blueblueblue2005 wrote:
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.


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

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 24 '05 #9

P: n/a
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.

Jul 24 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.