0

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

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

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

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++ 链接器和链接器错误的材料,但我无法弄清楚这一点。我总是因为我正在做的愚蠢事情而收到链接器错误。任何人都可以发现问题吗?

谢谢

4

1 回答 1

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

该方法尚未在任何地方实施。如果你的意思是它是纯虚拟的,那么你需要= 0在最后添加。

于 2014-12-08T08:08:03.713 回答