tamplate class error: "undefined reference"

Discussion in 'C++' started by s.z.s@web.de, Dec 5, 2006.

  1. Guest

    Hi!

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

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

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

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

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

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

    template<typename 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>::saywhat()'
    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.cc" 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
    , Dec 5, 2006
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Dec 5, 2006
    #2
    1. Advertising

  3. * :
    >
    > 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?
    Alf P. Steinbach, Dec 5, 2006
    #3
  4. Guest

    Thanks, and sorry for not checking the faq!

    steffen
    , Dec 5, 2006
    #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. R
    Replies:
    1
    Views:
    534
    Joris Gillis
    Jan 15, 2005
  2. Lu
    Replies:
    1
    Views:
    561
    Roel Schroeven
    Jul 10, 2003
  3. RU
    Replies:
    2
    Views:
    7,957
  4. LaBird
    Replies:
    2
    Views:
    6,888
    LaBird
    Jan 9, 2004
  5. Replies:
    10
    Views:
    766
    Kenny McCormack
    May 30, 2005
Loading...

Share This Page