3

このコードをビルドすると、リンカー エラーが発生します。

Exclude.h ファイル

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_;
};

同じファイル内:

/*
 * 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;
}

前の呼び出しを使用する cpp ファイルは次のようになります。

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
}

コードは正常にコンパイルされますが、リンカーからエラーが発生しました: Undefined first referenced symbol in file bool IsExclude::operator()(const __type_0*) MyFile.o

ヒントや提案はありますか?

PSメモリリークを避けるために、ベクトルをクリーンアップする必要があることは承知しています。コンパイラで boost::shared_ptr を使用できません。はぁ!

4

1 に答える 1

3

isExclude関数では、次のように記述します。

if ( (*pos)->operator()(toCheck) == true )

これIsExclude::operator()は宣言されているが定義されていない呼び出しであるため、リンカには文句を言う十分な理由があります。ポリモーフィックな動作をしたいと思っているようoperator()ですが、「テンプレート関数は仮想化できません」というトラップに陥ったようです。

あなたの要件が何であるかを知らずにあなたをもっと助けるのは難しいです、しかし多分あなたはテンプレートを持っていることを再考してそして仮想operator()に行くべきです。 operator()

于 2010-12-08T12:06:52.247 に答える