链接器错误 C++“未定义的引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14557657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:26:48  来源:igfitidea点击:

Linker Error C++ "undefined reference "

c++referenceundefined

提问by Fox

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

可能的重复:
什么是未定义的引用/未解析的外部符号错误,我该如何解决?

Trying to compile my program via g++ -o prog1 main.cpp -std=c++0x

试图通过编译我的程序 g++ -o prog1 main.cpp -std=c++0x

I get the error:

我收到错误:

/tmp/cc1pZ8OM.o: In function `main':
main.cpp:(.text+0x148): undefined reference to `Hash::insert(int, char)'
collect2: error: ld returned 1 exit status

main.cpp

主程序

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <functional>
#include "Hash.h"

using namespace std;

int main(int argc, char *argv[]) {
//preset prime number 
int prime = 101;
hash<char> h1;
int key;
Hash HashTable;

// check for Request & string parameters
if(argc != 3) {
    cout << "Run program with 2 parameters. [Lower Case]" << endl;
    cout << "[1] insert, find, or delete" << endl;
    cout << "[2] string" << endl;
}

if(strcmp(argv[1], "insert") == 0) {
    //Get Hash for argv[2] aka value
    key = h1(*argv[2]);

    //check 1
    cout << "Hash: " << key << endl;

    key = key % prime;

    //check 2
    cout << "Mod 101 Hash: " << key << endl;

    HashTable.insert(key, *argv[2]); //PROBLEM here

}

return 0;
}

Hash.h file:

哈希.h文件:

#include <iostream>
#include <cstring>
#include "LinkedList.h"
using namespace std;

class Hash {
//100 slot array for hash function
LinkedList *hashFN[100];

public:
void insert(int key, char value);
//void deleteItem(int key);
//char* find(int key);


};

Any ideas? Using this to build a hash table with set size.

有任何想法吗?使用它来构建一个具有设定大小的哈希表。

Edit: Hash.cppfile

编辑:Hash.cpp文件

#include <iostream>
#include <cstring>
#include "Hash.h"

using namespace std;

void Hash::insert(int key, char value){
*hashFN[key]->addFront(value);
cout << "Success!" << endl;

}

Trying to compile via terminal now with:

现在尝试通过终端编译:

g++ -c Hash.cpp -o Hash.o

g++ -o prog1 main.cpp Hash.o -std=c++0x

g++ -c Hash.cpp -o Hash.o

g++ -o prog1 main.cpp Hash.o -std=c++0x

It goes into an infinite loop somehow.

它以某种方式进入无限循环。

回答by sheu

Your header file Hash.hdeclares "what class hashshould look like", but not its implementation, which is (presumably) in some other source file we'll call Hash.cpp. By including the header in your main file, the compiler is informed of the description of class Hashwhen compiling the file, but not how class Hashactually works. When the linker tries to create the entire program, it then complains that the implementation (toHash::insert(int, char)) cannot be found.

您的头文件Hash.h声明了“class hash应该是什么样子”,而不是它的实现,它(大概)在我们将调用的其他一些源文件中Hash.cpp。通过在主文件中包含头文件,编译器会被告知class Hash编译文件时的描述,而不是class Hash实际工作方式。当链接器尝试创建整个程序时,它会抱怨toHash::insert(int, char)无法找到实现 ( )。

The solution is to link all the files together when creating the actual program binary. When using the g++ frontend, you can do this by specifying all the source files together on the command line. For example:

解决方案是在创建实际程序二进制文件时将所有文件链接在一起。使用 g++ 前端时,您可以通过在命令行上一起指定所有源文件来完成此操作。例如:

g++ -o main Hash.cpp main.cpp

will create the main program called "main".

将创建名为“main”的主程序。

回答by Kos

This error tells you everything:

这个错误告诉你一切:

undefined reference toHash::insert(int, char)

对Hash::insert(int, char)的未定义引用

You're not linking with the implementations of functions defined in Hash.h. Don't you have a Hash.cppto also compile and link?

您没有与Hash.h. 你不是也有Hash.cpp编译和链接吗?

回答by Saqlain

Your error shows you are not compiling file with the definition of the insertfunction. Update your command to include the file which contains the definition of that function and it should work.

您的错误表明您没有使用insert函数定义编译文件。更新您的命令以包含包含该函数定义的文件,它应该可以工作。