473,466 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

why doesn't this template work

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();
}
Jul 22 '05 #1
3 1636
"Steve" <sl*@usc.edu> wrote in message
news:7b**************************@posting.google.c om...
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/techt...#whylinkerror).

--
David Hilsee
Jul 22 '05 #2
Steve wrote:
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

Jul 22 '05 #3
On 22 Sep 2004 17:16:29 -0700, sl*@usc.edu (Steve) wrote:
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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lawrence | last post by:
I've a template with some PHP code in it. I need to get the names of all the PHP commands, so I can import them and so I can make sure they are officially allowed (for security purposes, users are...
1
by: Yereth | last post by:
Hi again, still working on my xslt program and I ran into another problem. There is a file which a contains the following structure: <key>1</key> <dict>         ... </dict> <key>2</key>
0
by: Oliver Kowalke | last post by:
Hi, following code will not compile with MS VC 7.1: struct null_type; template< bool B, typename T, typename E > struct if_then_else; template< typename T, typename E >
4
by: PengYu.UT | last post by:
The following shows the source code and the error message(from g++-3.3). I wrote two assignment operator. One is for the same type case, the other one is for different type case. I'm wondering why...
4
by: cody | last post by:
I have the following feed which I want to convert to html: http://www.heise.de/newsticker/heise.rdf but it does not work, I only see the html elements empty as if it found no elements in the xml...
2
by: jasm | last post by:
hello everybody! I have used reinterpret_cast for interpret a class object as a char*. This is the object: template<class T> class Coordinates { public: T *x; T *y;
2
by: MikhailGorbachev | last post by:
I'm having a problem getting derived types to be inferred as valid parameters by a function template. template <typename T> struct Base {} template<typename T> struct Derived : public...
8
by: Phil Endecott | last post by:
Dear Experts, I'm surprised to find that std::max doesn't work (i.e. won't compile) if the arguments are not of exactly the same type, e.g. one is a short and the other is a long: #include...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
3
by: brendanmcdonagh | last post by:
Hi all I have managed to suss out Sockets today and i have got a gui going with netbeans that sends text on button press to a serversocket on another computer. I receive the messages when i open...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.