开发者

LNK2019 error c++ unresolved external symbol

开发者 https://www.devze.com 2023-02-27 02:07 出处:网络
Ive got these error messages: Error 1error LNK2019: unresolved external symbol \"public: void __thiscall ArrayIntStorage::sortOwn(void)\"

Ive got these error messages:

Error 1 error LNK2019: unresolved external symbol "public: void __thiscall ArrayIntStorage::sortOwn(void)" (?sortOwn@ArrayIntStorage@@QAEXXZ) referenced in function _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

Error 2 error LNK2019: unresolved external symbol "public: void __thiscall ArrayIntStorage::sortStd(void)" (?sortStd@ArrayIntStorage@@QAEXXZ) referenced in function _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

Error 3 error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class ArrayIntStorage const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVArrayIntStorage@@@Z) referenced in function _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

Error 4 error LNK2019: unresolved external symbol "class std::basic_istream > & __cdecl operator>>(class std::basic_istream > &,class ArrayIntStorage &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVArrayIntStorage@@@Z) referenced in function _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

Error 5 error LNK2019: unresolved external symbol "public: bool __thiscall ArrayIntStorage::setReadSort(bool)" (?setReadSort@ArrayIntStorage@@QAE_N_N@Z) referenced in function _main G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\main.obj C_Style_Array

Error 6 error LNK1120: 5 unresolved externals G:\08227\ACW\MAIN\08227_ACW2_Test_Harnesses_2010-11\C_Style_Array\Debug\C_Style_Array.exe 1 1 C_Style_Array

and I dont know whats going on, I wonder if ive missed something? I am new to this and Its not giving me any line numbers so Im not sure which code to give you so Ill give you this part

#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 << arrayStor开发者_C百科age1;

fin1.close();
out1.close();
out2.close();


Your linker (part of the compiler) cannot find where ArrayIntStorage::sortOwn() is defined.

This usually occurs either:

  1. The definition of ArrayIntStorage::sortOwn() is in another .c file which you forgot to tell the compiler about (and so wasn't compiled),
  2. ArrayIntStorage is a header only library (so there is no other .c file), in which case you probably have forgotten to implement the function sortOwn(), and have only declaired it.
  3. ArrayIntStorage is an external library which has not been linked. (as Tomalak Geretkal Notes, and is solved by following the steps set out by paxdiablo)

If its neither of these, or you find these options confusing, please post the header file ArrayIntStorage.h and the corresponding .c file (should there be one).


This is a linker error and can be easily resolved once you understand the process.

By #include-ing the header file in your source code, that lets the compiler know about the definitions it requires.

However, there's an extra step needed. You have to link all the various object files and libraries together.

That's because, while the header contains information about the ArrayIntStorage stuff, the actual code for it is elsewhere. That's what's bought in at the link stage.

Basically, you need to ensure that the object file or library is included in your build process.

For example, the following gcc command will include the abc.o object module and bring in anything needed from the libxyz.a archive library:

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

It may also be done differently for different environments. For example, an IDE will most likely have it under project settings of some sort.

This answer provides some more information of the compiling and linking processes common in a lot of environments.


You forgot to link the library where ArrayIntStorage's functions are defined. Read the documentation for that library to find out how to use it in your project.


It appears that the ArrayIntStorage is compiled into a library. Check your project's Linker details, and add the library.


Probably not relevant for the code in this question, but you can also get this error if you forget to export a function declaration in a library and then try to use that function from outside that library. For example, this code

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

should become

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

精彩评论

暂无评论...
验证码 换一张
取 消