我无法弄清楚如何修复此未定义的引用错误。我有类hashTable
并且在其insertWord(string)
函数定义中,我调用了一个vector中对象的函数。问题是我收到了这个错误:
错误:
C:\Users\...\hashTable.o:hashTable.cpp|| undefined reference to `linkedList::linkedList()'|
C:\Users\...\hashTable.o:hashTable.cpp|| undefined reference to `linkedList::appendNode(std::string)'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
我认为它可能与hashTable类中的linkedList实例/函数的作用域有关。另外,我有一个linkedList.cpp和一个linkedList.h,它们都100%完美(很多测试),所以我不相信显示linkList的文字代码是有益的。
我的一些代码:
hashTable.h
///hashTable.h
#ifndef HASHTABLE_H_EXISTS
#define HASHTABLE_H_EXISTS
#include <string>
#include <vector>
#include "linkedList.h"
using namespace std;
//class hashTable
class hashTable{
private:
vector < linkedList > hTable; //our hashTable "storage system" ---> vector of linkedList objects
public:
hashTable();
hashTable(string);
void init_hashTable(); // general initializer function
int getIndex(string); //our "hashing" function
void loadFile(string);
void insertWord(string); // insert word into: vec< LList(word) >
};//end hashTable def
#endif
hashTable.cpp
///hashTable.cpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctype.h>
#include <string>
#include <vector>
#include "hashTable.h"
#include "linkedList.h"
using namespace std;
//class hashTable functions
hashTable::hashTable(){
//constructor
hashTable::init_hashTable();
}//end constructor
hashTable::hashTable(string fileName){
//overloaded constructor
hashTable::init_hashTable();
hashTable::loadFile(fileName);
}//end overloaded constructor
void hashTable::init_hashTable(){
// general inializations
// add a linkedList to the vector for each english letter
for (int i = 0;i < 26;i++)
hashTable::hTable.push_back( linkedList() );
}//end init_hashTable
int hashTable::getIndex(string word){
//gets index for hTable
// Returns ascii integer value of capitalized
// 1st letter minus ascii value of 'A'.
return toupper( word.at(0) ) - 65;
}//end getIndex
void hashTable::loadFile(string fileName){
// loads words by line from file: fileName
// our token
string token = "";
// input file stream
ifstream file;
file.open( fileName.c_str() );
// while not at endOfFile
while ( !file.eof() ){
// token gets current line/word
getline(file, token);
// if line is not empty
if (token != "")
// insert word into hashtable
hashTable::insertWord(token);
} // end while
}//end loadFile
void hashTable::insertWord(string word){
// Appends word to a LinkedList of cooresponding
// hash code position in the hTable (a=0,b=1,...,z=25).
// get index for LL in hTable
int hashCode = hashTable::getIndex(word);
// adding the word to our chosen linkedList
hashTable::hTable[hashCode].appendNode(word);
/* TAKE NOTE: //
\\ hTable[hashCode] is a specific \\
// linked list from the hTable vector */
}//end insertWord
// testing harness...
int main(){
hashTable tbl("words.txt");
return 0;
}//end main
答案 0 :(得分:0)
想出来:
1)你可以制作一个makefile;我使用PuTTY来完成此操作。
2)因为我们希望makefile是&#34; extension-less&#34;所以在Unix中生成makefile是理想的。
3)在这种情况下你会如何写它(在PuTTY中,使用vi编辑器):
vi makefile [PRESS ENTER]
HT: hashTable.o linkedList.o main.o
g++ hashTable.o linkedList.o main.o -o HT
hashTable.o: hashTable.cpp hashTable.h linkedList.h
g++ -c hashTable.cpp
linkedList.o: linkedList.cpp linkedList.h
g++ -c linkedList.cpp
main.o: main.cpp hashTable.h linkedList.h
g++ -c main.cpp
clean:
rm -f *.o
:wq [PRESS ENTER] // this will save and quit from the vi editor
这里的基本设置是:
Label: all object files seperated with a space
g++ all object files seperated with a space -o Label
objectFile.o: objectFile.cpp dependencies.h ...etc.
g++ -c objectFile.cpp
//...do above 2 lines for every object file(.o) you have
clean:
rm -f *.o // get rid of extension of your make file
然后在写完你的makefile之后,你只需输入&#39; make&#39;在目录中。然后,您将在与makefile相同的目录中看到您的Label(您的makefile中使用的Label)。然后你就可以运行你的&#34;一体机&#34;通过键入以下程序:./Label
如果你不想制作一个makefile,你可以直接输入PuTTY:
g++ -o HT hashtable.cpp linkedList.cpp main.cpp [PRESS ENTER]
但是,每次更改代码时都必须输入,而不是makefile。