tamplate class error: "undefined reference"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s.z.s@web.de

    tamplate class error: "undefined reference"

    Hi!

    I hope the solution to that is not too stupid... I've got three files:

    <snip test_main.cc>
    #include"test.h h"
    int main(void)
    {
    A<inta1;
    a1.saywhat();
    return 0;
    }
    </snip>

    <snap test.hh>
    #ifndef _A_HH_
    #define _A_HH_
    #include<iostre am>
    using namespace std;
    template<typena me T>
    class A
    {
    public:
    A();
    virtual ~A();
    void saywhat();
    private:
    double m_a;
    };
    #endif
    </snap>

    <snup test.cc>
    #include"test.h h"

    template<typena me T>
    A<T>::A()
    {
    m_a=23;
    cout << "const" << endl;
    }

    template<typena me T>
    A<T>::~A()
    {
    cout << "dest" << endl;
    }

    template<typena me T>
    void A<T>::saywhat( )
    {
    cout << "saywhat:" << m_a << endl;
    }
    </snup>

    when I compile with g++ version 4.1.2 20061028
    # g++ test.cc test_main.cc
    I get the following errors

    /tmp/cc7Ejh47.o: In function `main':
    test_main.cc:(. text+0x8b): undefined reference to `A<int>::A()'
    test_main.cc:(. text+0x96): undefined reference to `A<int>::saywha t()'
    test_main.cc:(. text+0xa6): undefined reference to `A<int>::~A()'
    test_main.cc:(. text+0xbc): undefined reference to `A<int>::~A()'
    collect2: ld returned 1 exit status

    interestingly, when I #include"test.c c" in the test_main.cc, the
    program compiles and runs fine! But that's not how it's supposed to
    be...

    What the $(%&§)$ am I doing wrong?

    Thanks in advance!

    Steffen

  • Victor Bazarov

    #2
    Re: tamplate class error: &quot;undefi ned reference&quot;

    s.z.s@web.de wrote:
    [...link errors when placing templates in separate TU...]
    >
    What the $(%&§)$ am I doing wrong?
    You're not reading the FAQ before posting.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Alf P. Steinbach

      #3
      Re: tamplate class error: &quot;undefi ned reference&quot;

      * s.z.s@web.de:
      >
      What the $(%&§)$ am I doing wrong?
      FAQ item 35.12.

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • s.z.s@web.de

        #4
        Re: tamplate class error: &quot;undefi ned reference&quot;

        Thanks, and sorry for not checking the faq!

        steffen

        Comment

        Working...