Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > tamplate class error: "undefined reference"

Reply
Thread Tools

tamplate class error: "undefined reference"

 
 
s.z.s@web.de
Guest
Posts: n/a
 
      12-05-2006
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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-05-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-05-2006
* (E-Mail Removed):
>
> 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?
 
Reply With Quote
 
s.z.s@web.de
Guest
Posts: n/a
 
      12-05-2006
Thanks, and sorry for not checking the faq!

steffen

 
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
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
XSLT one tamplate opens tag second template closes - newbie R XML 1 01-15-2005 02:48 PM
instanciate a class in a jar file with class.forname, while my main class is in another jar cyril Java 2 08-25-2004 06:55 AM



Advertisments