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

查看:246
本文介绍了链接器错误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

main.cpp

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文件

#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 :: insert(int,char)

undefined reference toHash::insert(int, char)

您不是与 Hash.h中定义的函数的实现。你有没有 Hash.cpp 也可以编译和链接?

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

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

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