我正在研究RPN表达式的计算器,我收到的链接器错误似乎无法解决。我正在尝试使用双重调度来选择正确的操作和值。我将从类层次结构的顶部开始向下移动直到到达Operand。它不是来自运营商。
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 );
};
下一个派生类是运算符,然后是运算符标题中的加法:
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);
};
Integer派生自操作数,这是标题中类定义的一部分:
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 ++链接器和链接器错误的大量内容,但我无法解决这个问题。对于我正在做的愚蠢事情,我总是会收到链接器错误。有谁能发现问题?
由于
答案 0 :(得分:2)
class Operand : public Token
{
// ....
virtual Operand::pointer_type perform_addition(TokenList& params);
此方法尚未在任何地方实施。如果您认为它是纯虚拟的,那么您需要在最后添加= 0
。