Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why doesn't this template work

Reply
Thread Tools

why doesn't this template work

 
 
Steve
Guest
Posts: n/a
 
      09-23-2004
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+0x3:template_main.cpp:
undefined reference to `data<int>:rint()'
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>:rint(){
std::cout << d << std::endl;
}

// the code, third file: template_main.cpp
----------------------------------
#include "template_test.h"
int main(){
data<int> d(3);
d.print();
}
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-23-2004
"Steve" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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+0x3:template_main.
cpp:
> undefined reference to `data<int>:rint()'
> 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>:rint(){
> 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


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      09-23-2004
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

 
Reply With Quote
 
Tom Widmer
Guest
Posts: n/a
 
      09-23-2004
On 22 Sep 2004 17:16:29 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments