未解析的外部符号,尝试修复时遇到问题

unresolved external symbol, having trouble trying to fix

本文关键字:问题 遇到 外部 符号      更新时间:2023-10-16

您好,我从下面的代码中收到此错误:

Error 4 error LNK2019: unresolved external symbol "public: __thiscall Noun::Noun(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Noun@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "public: __thiscall Dictionary::Dictionary(void)" (??0Dictionary@@QAE@XZ)

我想这个错误来自没有声明构造函数Noun(string word, string definition);我也不确定如何声明它以消除错误,以便我可以编译我的代码。下面是我的代码。

链接器抱怨您没有为 Noun 的构造函数添加定义。添加它,此错误将被修复。我相信你的意思只是在其中调用父级的构造函数,所以也许这会完成这项工作:

Noun(string word, string definition) : Word(word, definition) {}

您可以定义构造函数

Noun(string word, string definition)

与您使用类Word的构造函数所做的完全相同。但是你需要确保在使用它时知道名词的定义。通常,为此使用单独的头文件。类 Noun 的头文件的内容已经包含在您给出的代码中: 它以

class Noun : public virtual Word {

并以

};

将其移动到名为 noun.h 的头文件中,并通过以下方式将其包含在您的 cpp 文件中

#include "noun.h"