链接器错误 C++“未定义引用" [英] Linker Error C++ "undefined reference "

查看:46
本文介绍了链接器错误 C++“未定义引用"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
什么是未定义引用/未解决的外部符号错误以及如何修复它?

尝试通过 g++ -o prog1 main.cpp -std=c++0x

我收到错误:

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

ma​​in.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 文件:

#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.

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++ -c Hash.cpp -o Hash.o

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

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

不知何故进入了无限循环.

It goes into an infinite loop somehow.

推荐答案

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

Your header file Hash.h declares "what class hash should 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 Hash when compiling the file, but not how class Hash actually works. When the linker tries to create the entire program, it then complains that the implementation (toHash::insert(int, char)) cannot be found.

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

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++ -o main Hash.cpp main.cpp

将创建名为main"的主程序.

will create the main program called "main".

这篇关于链接器错误 C++“未定义引用"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆