473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

template and inheritance

Hi, I am having trouble compiling the simple code below...

//leader.hpp
#ifndef LEADER_HPP
#define LEADER_HPP
class leader
{
public:
leader(){}
virtual ~leader(){}
virtual void hahaha()=0;
virtual void hahahaha()=0;
};
#endif

//soldier.hpp
#ifndef SOLDIER_HPP
#define SOLDIER_HPP
#include "leader.hpp"

template <class T>
class soldier : public leader
{
public:
soldier(){}
virtual ~soldier(){}
virtual void hahaha();
virtual void hahahaha();
};
#endif
//soldier.cpp
#include "soldier.hpp"

template <class T>
void soldier<T>::hahaha()
{

}

template <class T>
void soldier<T>::hahahaha()
{

}

// test.cpp
#include "leader.hpp"
#include "soldier.hpp"

int main(void)
{
leader *a;

a=new soldier<int>();

delete a;

return 0;
}

when I compile with g++, I get these errors:

g++ -o "test" test.o soldier.o
test.o:(.rodata._ZTV7soldierIiE[vtable for soldier<int>]+0x10): undefined reference to `soldier<int>::hahaha()'
test.o:(.rodata._ZTV7soldierIiE[vtable for soldier<int>]+0x14): undefined reference to `soldier<int>::hahahaha()'
collect2: ld returned 1 exit status

Is there something wrong with the code??
thanks,
sunny
Aug 30 '08 #1
2 1289
Airslash
221 100+
I'm going to take a guess here, but you define Soldier as a Template class, but your Constructor doesn't support templates.
Aug 30 '08 #2
newb16
687 512MB
You should either explicit instantiate soldier<int> in soldier.cpp or move soldier<T>::haha() body to header file - compiler doesn't know at the time it compiles soldier.cpp that there will be soldier<int> in main.cpp.
Aug 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Gandu | last post by:
Could some C++ guru please help me? I have a very odd problem with respect templates and inheritance. I have templatized List class, from which I am inheriting to create a Stack class. All works...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
3
by: Thomas Matthews | last post by:
Hi, I would like to apply inheritance to a template parameter, but my design fails to compile: cannot initialize one template class with child child parameterized class. I'll explain... ...
2
by: Tony Johansson | last post by:
Hello Experts To derive a concrete class from a template class in invalid. Why?? So you should not be able have something like this class Derived : public Base<int, 100> It's valid to derive...
1
by: Tony Johansson | last post by:
Hello Experts! I reading a book called programming with design pattern revealed by Tomasz Muldner and here I read something that I don't understand completely. Im I right if I say the...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
2
by: Thomas Kowalski | last post by:
Hi, I would like to write a template class Polygon<VertexTypthere vertex typ can be eigther a pointer or a value typ. It has an attribute: std::vector<VertexTypvertices; And a methode:...
9
by: stephen.diverdi | last post by:
Can anyone lend a hand on getting this particular template specialization working? I've been trying to compile with g++ 4.1 and VS 2005. ...
32
by: Stephen Horne | last post by:
I've been using Visual C++ 2003 for some time, and recently started working on making my code compile in GCC and MinGW. I hit on lots of unexpected problems which boil down to the same template...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.