使用模板的未定义符号

发布于 2024-10-07 02:21:29 字数 1729 浏览 0 评论 0原文

我在构建此代码时遇到链接器错误:

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
}

代码编译正常,但我从链接器收到错误: 未定义首次引用 文件中的符号 bool IsExclude::operator()(const __type_0*) MyFile.o

您有任何提示或建议吗?

PS我知道我需要清理向量以避免内存泄漏。我无法在编译器中使用 boost::shared_ptr 。叹!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2024-10-14 02:21:29

isExclude 函数中,您可以编写:

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

这会调用声明但未定义的 IsExclude::operator() ,因此链接器有充分的理由抱怨。在我看来,您希望operator()上具有多态行为,但您陷入了“模板函数不能是虚拟的”陷阱。

如果不知道您的要求是什么,就很难为您提供更多帮助,但也许您应该重新考虑使用模板化 operator() 并选择虚拟 operator()< /代码>。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文