I am getting a linker error building this code:
Exclude.h file
class IsExclude
{
public:
template<typename T>
bool operator()(const T* par);
virtual ~IsExclude() = 0;
};
IsExclude::~IsExclude() {}
class IsExcludeA : public IsExclude
{
public:
IsExcludeA(std::string toCompare) : toCompare_(toCompare) {}
template<typename T>
bool operator()(const T* par)
{
return strcmp(par->Something, toCompare_.c_str() ) ? false : true ;
}
~IsExcludeA() {}
private:
std::string toCompare_;
};
In the same file:
/*
* loop over a container of function objects
* if at least one of them return true the function
* return true, otherwise false
* The function was designed to evaluate a set of
* exclusion rule put in "and" condition.
*/
template<typename T,typename P>
bool isExclude( const T& cont, const P* toCheck )
{
typename T::const_iterator pos;
typename T::const_iterator end(cont.end());
bool ret(false);
for (pos = cont.begin(); pos != end; ++pos)
{
if ( (*pos)->operator()(toCheck) == true )
{
ret = true;
pos = end;
}
}
return ret;
}
The cpp file where I use the previous call looks like this:
std::vector<IsExclude* > exVector;
exVector.push_back( new IsExcludeA(std::string("A")) );
exVector.push_back( new IsExcludeA(std::string("B")) );
if (isExclude(exVector,asset) == false)
{
// Blah
}
The code compile fine but I got an error from the linker: Undefined first referenced symbol in file bool IsExclude::operator()(const __type_0*) MyFile.o
Do you have any hint or suggestions?
P.S. I am aware I need to clean up the vector in order to avoid memory leak. I can't use boost::shared_ptr with my compiler. Sigh!
In the isExclude function, you write :
if ( (*pos)->operator()(toCheck) == true )
This calls IsExclude::operator() which is declared but not defined, so the linker has a good reason to complain. It seems to me that you would like to have a polymorphic behavior on operator() but that you fell into the 'template functions cannot be virtual' trap.
It's hard to help you more without knowing what are your requirements, but maybe you should reconsider having a templated operator() and go for a virtual operator().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With