Need help on template

A

Anuja

Hi,


I have a template question

Header
template <class T>
class BaseClass {
public:
void isValid(T aId);
};

Cpp file
template <class T>
class BaseClass {
BaseClass::isValid(T aId) {
cout << aId << endl;
}
}


Derived class header
class DerivedClass: public BaseClass<short> {
};

It gives compile-time error saying undefined symbol
BaseClass<short>::isValid(short)

Isnt isValid method inherited ?

Thanks
Jay
 
I

Ian Collins

Anuja said:
Hi,


I have a template question

Header
template <class T>
class BaseClass {
public:
void isValid(T aId);
};

Cpp file
template <class T>
class BaseClass {
BaseClass::isValid(T aId) {
cout << aId << endl;
}
}


Derived class header
class DerivedClass: public BaseClass<short> {
};

It gives compile-time error saying undefined symbol
BaseClass<short>::isValid(short)

Isnt isValid method inherited ?
Your compiler probably requires the method definition to be included in
the header.
 
B

benben

Anuja said:
Hi,


I have a template question

Header
template <class T>
class BaseClass {
public:
void isValid(T aId);
};

Cpp file
template <class T>
class BaseClass {
BaseClass::isValid(T aId) {
cout << aId << endl;
}
}

Put whatever in the Cpp file back to the header. Read the FAQ and other
related posts to find out why.
Derived class header
class DerivedClass: public BaseClass<short> {
};

It gives compile-time error saying undefined symbol
BaseClass<short>::isValid(short)

Isnt isValid method inherited ?

It should have been link-time error? Are you positive that it is indeed
a compile time error?
Thanks
Jay

Regards,
Ben

P.S. I am just curious if you guys are all from the same
class/college/school. It has been a lot of post on the same topic since
the last three days.
 
B

benben

Oops, my previous reply seems like a premature judgment. Please ignore
it. Sorry.

With what you posted I can't pinpoint the exact error. Perhaps post more
code would help.

regards,
Ben
 
A

ak

The template class definition is not right. It'd go like:


template <class T>
void BaseClass<T>::isValid(T aId) {
std::cout << aId << std::endl;
};


Besides, make sure this template class definition is visible to the c++
compiler whenever a DerivedClass is declared.

And use g++ instead of gcc. (If you are using gnu)

Cheers,
AK
 
A

ak

I guess to make myself clearer, I better post the way I think it could
be done. Of course there is always more than one way to do it:

template <class T>
class BaseClass {
public:
void isValid(T aId);
};
#include <iostream>
#include <string>

#include "bc.hpp"

template <class T>
void BaseClass<T>::isValid(T aId) {
std::cout << aId << std::endl;
};
#include "bc.tpl"

#include "dc.hpp"

int main(int, char**) {

DerivedClass a;
a.isValid(1);

}

Hope these helps.

Cheers,
AK
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top