473,409 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

undefined template reference again ??

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
9 4827
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
I compiled all the .cpp file

g++ t.cpp Listnode.cpp

Jul 24 '05 #3
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
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
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
well, neither way works :(

Jul 24 '05 #7
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: LaBird | last post by:
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 --...
3
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...
6
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...
2
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...
8
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...
1
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...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
3
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
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.