链接错误c++“未定义引用”[复制]

时间:2022-11-25 05:38:01

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化= c++ 0 x

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

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.cpp file

编辑:散列。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散列。cpp - o Hash.o

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

g++ - o prog1主要。cpp散列。o化= c++ 0 x

It goes into an infinite loop somehow.

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

4 个解决方案

#1


26  

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.

你的头文件哈希。h声明“类哈希应该是什么样子”,而不是它的实现,这是(大概)在其他一些源文件中,我们称之为Hash.cpp。通过在主文件中包含header,编译器在编译文件时被告知类散列的描述,但不知道类哈希实际上是如何工作的。当链接器试图创建整个程序时,它会抱怨这个实现(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”。

#2


24  

This error tells you everything:

这个错误告诉你一切:

undefined reference toHash::insert(int, char)

未定义的参考toHash::插入(int、char)

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?

您没有链接到Hash.h中定义的函数的实现。难道你没有哈希吗?cpp还可以编译和链接吗?

#3


1  

Where is Hash.cpp file? Where is definition of Hash::insert function?

散列。cpp文件?散列的定义在哪里::插入函数?

#4


0  

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

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

#1


26  

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.

你的头文件哈希。h声明“类哈希应该是什么样子”,而不是它的实现,这是(大概)在其他一些源文件中,我们称之为Hash.cpp。通过在主文件中包含header,编译器在编译文件时被告知类散列的描述,但不知道类哈希实际上是如何工作的。当链接器试图创建整个程序时,它会抱怨这个实现(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”。

#2


24  

This error tells you everything:

这个错误告诉你一切:

undefined reference toHash::insert(int, char)

未定义的参考toHash::插入(int、char)

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?

您没有链接到Hash.h中定义的函数的实现。难道你没有哈希吗?cpp还可以编译和链接吗?

#3


1  

Where is Hash.cpp file? Where is definition of Hash::insert function?

散列。cpp文件?散列的定义在哪里::插入函数?

#4


0  

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

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