why doesn't this template work

Discussion in 'C++' started by Steve, Sep 23, 2004.

  1. Steve

    Steve Guest

    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();
    }
     
    Steve, Sep 23, 2004
    #1
    1. Advertising

  2. Steve

    David Hilsee Guest

    "Steve" <> wrote in message
    news:...
    > 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).

    --
    David Hilsee
     
    David Hilsee, Sep 23, 2004
    #2
    1. Advertising

  3. Steve

    Rolf Magnus Guest

    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
     
    Rolf Magnus, Sep 23, 2004
    #3
  4. Steve

    Tom Widmer Guest

    On 22 Sep 2004 17:16:29 -0700, (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
     
    Tom Widmer, Sep 23, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Prowak

    Why oh why doesn't my data view work?

    David Prowak, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    752
    Alvin Bruney [MVP]
    Jan 30, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    933
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,127
    Smokey Grindel
    Dec 2, 2006
  4. Sara
    Replies:
    6
    Views:
    281
    John W. Krahn
    Apr 12, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    278
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page