LNK2019错误c++未解析的外部符号

LNK2019 error c++ unresolved external symbol

本文关键字:外部 符号 错误 c++ LNK2019      更新时间:2023-10-16

我得到了这些错误信息:

错误1错误LNK2019: unresolved外部符号"public: void"__thiscall ArrayIntStorage: sortOwn(空白)"(? sortOwn@ArrayIntStorage@@QAEXXZ)函数引用_main G: 08227 主要联队 08227 _acw2_test_harnesses_2010-11 C_Style_Array 主要。obj C_Style_Array

错误2错误LNK2019: unresolved外部符号"public: void"__thiscall ArrayIntStorage: sortStd(空白)"(? sortStd@ArrayIntStorage@@QAEXXZ)函数引用_main G: 08227 主要联队 08227 _acw2_test_harnesses_2010-11 C_Style_Array 主要。obj C_Style_Array

错误3错误LNK2019: unresolved外部符号"类Std::basic_ostream> &__cdecloperator<& lt;(类Std::basic_ostream> &ampArrayIntStorage &)(? ? 6 @yaaav basic_ostream@DU美元? char_traits@D@std@@@std@@AAV01@ABVArrayIntStorage@@@Z美元)函数引用_main G: 08227 主要联队 08227 _acw2_test_harnesses_2010-11 C_Style_Array 主要。obj C_Style_Array

错误4错误LNK2019: unresolved外部符号"类Std::basic_istream> &__cdecl操作符>>(类Std::basic_istream> &ampArrayIntStorage,)"(? ? 5 @yaaav basic_istream@DU美元? char_traits@D@std@@@std@@AAV01@AAVArrayIntStorage@@@Z美元)函数引用_main G: 08227 主要联队 08227 _acw2_test_harnesses_2010-11 C_Style_Array 主要。obj C_Style_Array

错误5错误LNK2019: unresolved外部符号"public":bool__thiscall ArrayIntStorage: setReadSort (bool)"(? setReadSort@ArrayIntStorage@@QAE_N_N@Z)函数引用_main G: 08227 主要联队 08227 _acw2_test_harnesses_2010-11 C_Style_Array 主要。obj C_Style_Array

错误6错误LNK1120: 5 unresolved外部G:8227ACWMAIN8227_ACW2_Test_Harnesses_2010-11C_Style_ArrayDebugC_Style_Array.exe 11 C_Style_Array

我不知道发生了什么,我想知道我是否错过了什么?我是新手,它没有给我任何行号,所以我不确定给你哪个代码,所以我给你这部分

#include <fstream>
#include <iostream>
using namespace std;
#include "ArrayIntStorage.h"
int main(int argc, char **argv) {
ifstream fin1("ACW2_data.txt");
ofstream out1("1-arrayUnsortedRead.txt");
ofstream out2("2-arrayUnsortedRead-thenSTDSort.txt");
if(!fin1.is_open()) 
{
    cout << "FAIL" << endl;
    return 1;
}
ArrayIntStorage arrayStorage1;
arrayStorage1.setReadSort(false);   // do not read sort
// read in int values into data structure
fin1 >> arrayStorage1;
// output int values in data structure to file
out1 << arrayStorage1;
// sort data structure using std
arrayStorage1.sortStd();
// output int values in data structure to file
out2 << arrayStorage1;
fin1.close();
out1.close();
out2.close();

您的链接器(编译器的一部分)找不到ArrayIntStorage::sortOwn()的定义位置

这通常发生:

  1. ArrayIntStorage::sortOwn()的定义在另一个。c文件中,你忘了告诉编译器(所以没有编译),
  2. ArrayIntStorage是一个头唯一的库(所以没有其他。c文件),在这种情况下,你可能已经忘记实现函数sortOwn(),并只声明它。
  3. ArrayIntStorage是一个没有被链接的外部库。(如Tomalak Geretkal所述,并通过遵循paxdiablo设置的步骤来解决)

如果这两者都不是,或者你发现这些选项令人困惑,请发布头文件ArrayIntStorage.h和相应的。c文件(如果有一个)。

这是一个链接器错误,一旦你了解了这个过程,就可以很容易地解决。

通过#include -ing在你的源代码的头文件,让编译器知道它需要的定义。

但是,还需要一个额外的步骤。你必须把所有不同的目标文件和库链接在一起。

这是因为,虽然标题包含有关ArrayIntStorage的信息,但它的实际代码在其他地方。这是在链接阶段购买的。

基本上,您需要确保目标文件或库包含在您的构建过程中。

例如,下面的gcc命令将包含abc。从libxyz.a存档库中引入所需的任何内容:

gcc -o myprog myprog.c abc.o -L/path/to/libs -lxyz

对于不同的环境,也可能有不同的处理方法。例如,IDE很可能将它放在某种类型的项目设置中。

这个答案提供了许多环境中常见的编译和链接过程的更多信息。

您忘了链接定义ArrayIntStorage函数的库。请阅读该库的文档,以了解如何在项目中使用它。

看起来ArrayIntStorage被编译成一个库。检查项目的链接器详细信息,并添加库。

可能与这个问题中的代码无关,但是如果您忘记在库中导出函数声明,然后尝试从该库外部使用该函数,也会得到此错误。例如,以下代码

bool operator==(const Foo &a, const Foo &b);

应该成为

MY_EXPORT_MACRO bool operator==(const Foo &a, const Foo &b);