c++ - 我怎样才能找到这个令人难以置信的神秘链接器错误的原因?

标签 c++ visual-studio linker linker-errors lnk2001

我正在研究 RPN 表达式的计算器,我遇到了一个我似乎无法解决的链接器错误。我正在尝试使用双重分派(dispatch)来选择正确的操作和值。我将从类层次结构的顶部开始向下移动,直到到达操作数。它不是从运算符派生的。

integer.obj : error LNK2001: unresolved external symbol "public: virtual class std::shared_ptr<class Operand> __thiscall Operand::perform_addition(class std::vector<class std::shared_ptr<class Token>,class std::allocator<class std::shared_ptr<class Token> > > &)" (?perform_addition@Operand@@UAE?AV?$shared_ptr@VOperand@@@std@@AAV?$vector@V?$shared_ptr@VToken@@@std@@V?$allocator@V?$shared_ptr@VToken@@@std@@@2@@3@@Z)
tokenizer.obj : error LNK2001: unresolved external symbol "public: virtual class std::shared_ptr<class Operand> __thiscall Operand::perform_addition(class std::vector<class std::shared_ptr<class Token>,class std::allocator<class std::shared_ptr<class Token> > > &)" (?perform_addition@Operand@@UAE?AV?$shared_ptr@VOperand@@@std@@AAV?$vector@V?$shared_ptr@VToken@@@std@@V?$allocator@V?$shared_ptr@VToken@@@std@@@2@@3@@Z)
ut_rpn_evaluator.obj : error LNK2001: unresolved external symbol "public: virtual class std::shared_ptr<class Operand> __thiscall Operand::perform_addition(class std::vector<class std::shared_ptr<class Token>,class std::allocator<class std::shared_ptr<class Token> > > &)" (?perform_addition@Operand@@UAE?AV?$shared_ptr@VOperand@@@std@@AAV?$vector@V?$shared_ptr@VToken@@@std@@V?$allocator@V?$shared_ptr@VToken@@@std@@@2@@3@@Z)
tokenizer.obj : error LNK2001: unresolved external symbol "public: virtual class std::shared_ptr<class Operand> __thiscall Operation::perform(class std::vector<class std::shared_ptr<class Token>,class std::allocator<class std::shared_ptr<class Token> > > &)" (?perform@Operation@@UAE?AV?$shared_ptr@VOperand@@@std@@AAV?$vector@V?$shared_ptr@VToken@@@std@@V?$allocator@V?$shared_ptr@VToken@@@std@@@2@@3@@Z)
C:\Users\User\OneDrive\School\Semester 3\Course\project\Debug\ut_rpn_evaluator.exe : fatal error LNK1120: 2 unresolved externals

这些函数的定义从基类开始:

class Operation : public Token {
public:
    using pointer_type = TOKEN_PTR_TYPE(Operation);
    virtual unsigned number_of_args() const = 0;
    virtual Operand::pointer_type perform( TokenList& params );
};

下一个派生类是 Operator,然后是 Operator 头中的 Addition:

class Addition : public LAssocOperator {
    DEF_IS_CONVERTABLE_FROM(Addition)
    DEF_PRECEDENCE(ADDITIVE)
    public:
        Operand::pointer_type perform(TokenList& params);
    };

这是我在运算符源代码文件中的实现:

Operand::pointer_type Addition::perform(TokenList& param){
    Operand::pointer_type op;
        op = op->perform_addition(params);
    return op;

}

这是函数在操作数头文件中的声明:

class Operand : public Token
{
public:
    using pointer_type = TOKEN_PTR_TYPE(Operand);

    //Operation Definitions
    virtual Operand::pointer_type perform_addition(TokenList& params);
};

整数派生自操作数,这是 header 中类定义的一部分:

class Integer : public Operand {
public:
    //usings for pointer and value
private:
    //member for value
public:
    //Constructor
    value_type              get_value() const { return value_; }

        Operand::pointer_type perform_addition(TokenList& params);
};

最后,我最终在整数源文件中定义了函数:

Operand::pointer_type Integer::perform_addition(TokenList& params){
    Integer::value_type value = Integer::value_type(0);
    for (auto val : params){
        value += convert<Integer>(val)->get_value();
    }
    return make_operand<Integer>(value);
}

我已经阅读了很多关于 C++ 链接器和链接器错误的 Material ,但我无法弄清楚这一点。我总是因为我所做的愚蠢的事情而收到链接器错误。谁能发现问题?

谢谢

最佳答案

class Operand : public Token
{
    // ....
    virtual Operand::pointer_type perform_addition(TokenList& params);

此方法尚未在任何地方实现。如果你想让它成为纯虚拟的,那么你需要在最后添加 = 0

关于c++ - 我怎样才能找到这个令人难以置信的神秘链接器错误的原因?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27353698/

相关文章:

c++ - c++读取文本文件并将数字输入2D数组中

c++ - GCC 插件,添加新的优化编译指示

visual-studio - 如何根据其版本生成MSI软件包名称?

visual-studio - Visual Studio 2017 runsettings 视频捕获设置,wmv 0字节

c - gcc 无法在动态库中找到对对象的引用

c++ - 霍夫曼编码

c++ - C++/gcc/linux 中的 Continuations/Coroutines/Generators

asp.net - Visual Studio 2019 - 不兼容找不到此项目类型所基于的应用程序

c - GCC将常量指针和常量整数放入不同的内存区域

c - 如何使用 GCC 在绝对地址处声明变量?