3

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!

Martin J.
  • 5,028
  • 4
  • 24
  • 41
Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41
  • `return strcmp(par->Something, toCompare_.c_str() ) ? false : true ; ` should be written as `return par->Something == toCompare;` – user102008 Mar 18 '11 at 05:47

1 Answers1

3

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().

icecrime
  • 74,451
  • 13
  • 99
  • 111