tamplate class error: "undefined reference"

S

s.z.s

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top