why doesn't this template work

S

Steve

Sorry to be stupid, but I've been poring over TC++PL, and according to
what I see on page 351 (3rd ed.) the following code (split over three
files) should work. Instead, when I try to compile with g++ 3.3.3
(cygwin special), I get the following error. What am I doing wrong?

// the error: -----------------------------------------------
~/cpplib/tests[553] g++ template_test.cpp template_main.cpp
/c/DOCUME~1/SSCOTT~1.EDU/LOCALS~1/Temp/ccOJZwc5.o(.text+0x38):template_main.cpp:
undefined reference to `data<int>::print()'
collect2: ld returned 1 exit status


//the code, first file: template_test.h
----------------------------------
#ifndef TTEST_H
#define TTEST_H

template <class T>
class data{
public:
T d;
data(T x): d(x){}
void print();
};

#endif //TTEST_H

// the code, second file: template_test.cpp
------------------------------
#include "template_test.h"
#include <iostream>

export template <class T>
void data<T>::print(){
std::cout << d << std::endl;
}

// the code, third file: template_main.cpp
----------------------------------
#include "template_test.h"
int main(){
data<int> d(3);
d.print();
}
 
D

David Hilsee

Steve said:
Sorry to be stupid, but I've been poring over TC++PL, and according to
what I see on page 351 (3rd ed.) the following code (split over three
files) should work. Instead, when I try to compile with g++ 3.3.3
(cygwin special), I get the following error. What am I doing wrong?

// the error: -----------------------------------------------
~/cpplib/tests[553] g++ template_test.cpp template_main.cpp
/c/DOCUME~1/SSCOTT~1.EDU/LOCALS~1/Temp/ccOJZwc5.o(.text+0x38):template_main.
cpp:
undefined reference to `data<int>::print()'
collect2: ld returned 1 exit status


//the code, first file: template_test.h
----------------------------------
#ifndef TTEST_H
#define TTEST_H

template <class T>
class data{
public:
T d;
data(T x): d(x){}
void print();
};

#endif //TTEST_H

// the code, second file: template_test.cpp
------------------------------
#include "template_test.h"
#include <iostream>

export template <class T>
void data<T>::print(){
std::cout << d << std::endl;
}

// the code, third file: template_main.cpp
----------------------------------
#include "template_test.h"
int main(){
data<int> d(3);
d.print();
}

The last time I checked, g++ didn't support export. Move the definition of
print to the header. For more information, see the FAQ
(http://www.parashift.com/c++-faq-lite/) section 34 (Container classes and
templates), question 12 (Why can't I separate the definition of my templates
class from it's declaration and put it inside a .cpp file?) and others that
follow. Comeau's website has some good information too
(http://www.comeaucomputing.com/techtalk/templates/#whylinkerror).
 
R

Rolf Magnus

Steve said:
Sorry to be stupid, but I've been poring over TC++PL, and according to
what I see on page 351 (3rd ed.) the following code (split over three
files) should work. Instead, when I try to compile with g++ 3.3.3
(cygwin special), I get the following error. What am I doing wrong?

<example using export snipped>

The problem is that export is quite problematic to implement, therefore most
compilers still don't support it. But I wonder why the compiler doesn't
give a warning in this case. In a simple:

export template <typename T> void X(T)
{
}

int main()
{
}

it says:

export.cpp:1: warning: keyword `export' not implemented, and will be ignored
 
T

Tom Widmer

Sorry to be stupid, but I've been poring over TC++PL, and according to
what I see on page 351 (3rd ed.) the following code (split over three
files) should work. Instead, when I try to compile with g++ 3.3.3
(cygwin special), I get the following error. What am I doing wrong?

Currently export is only implemented on:

Comeau C++
Intel C++ (?, an undocumented switch)
Borland C++ (as yes unreleased )

GCC doesn't support it, and I don't think they've yet scheduled the
implementation time to do it (it is a very complex feature to
implement). If you want it, you'll have to say so on the GCC developer
mailing lists.

Tom
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top