473,386 Members | 1,715 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,386 software developers and data experts.

Need help on template

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

Apr 9 '06 #1
5 1301
Anuja wrote:
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.

--
Ian Collins.
Apr 9 '06 #2
Anuja wrote:
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.
Apr 9 '06 #3
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
Apr 9 '06 #4
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

Apr 9 '06 #5
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:

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

#include "bc.hpp"

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

class DerivedClass: public BaseClass<short> {
};
main.cpp

#include "dc.hpp"

int main(int, char**) {

DerivedClass a;
a.isValid(1);

}

Hope these helps.

Cheers,
AK

Apr 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bernard | last post by:
Hi, I have a problem with a CGI script (Perl) on a Win2000 server. The script is for sending E-cards and was written by Jason Maloney:...
1
by: David2511 | last post by:
Hello, I need a little help. I try to write the following architecture : an abstract template class A Two classes derived from class A : the classes B and C which are concrete. The class...
11
by: Micha | last post by:
Hello there, I think I've run into some classic c++ pitfall and maybe some of you guys can help me out. For my project I will need to use matrices and vectors and so I decided to implement them...
4
by: Piper707 | last post by:
I need help with using a general template which would process all tags other than the ones for which specific templates have been written. My XML looks like this: <ITEMS>...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
1
by: sommarlov | last post by:
Hi everyone >From one of our systems an xml file is produced. I need to validate this file before we send it to an external system for a very lenghty process. I cannot change the xml file layout....
3
by: vijaykokate | last post by:
Our company http://www.softnmation.com/ offers its customers a great variety of products. Everything you need can be found in this site. Web Template, CSS Template, Logo Template, Corporate...
1
by: rllioacvuher | last post by:
I need help with a program. I have implemented that following header file with an unordered list using one array, but i need to be able to use an ordered list and 2 arrays (one for the links and one...
5
by: aaragon | last post by:
Hello everybody, I appreciate your taking the time to take a look at this example. I need some help to start the design of an application. To that purpose I'm using policy-based design. The...
15
by: Jess | last post by:
Hello, Sometimes declarations are all what we need when we define/declare classes (or functions?), but sometimes we need definitions. I learned that if we define a class (B) that has an object...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.