我無法確定如何解決此問題未定義的引用錯誤。我有類hashTable
和它的insertWord(string)
函數定義,我調用一個位於向量中的鏈接列表對象的函數。問題是,我得到這個錯誤:從另一個對象調用一個對象的函數時發生C++未定義引用
錯誤:
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)) ===|
我想這可能是一些做作用域的LinkedList的實例/ Hashtable類中的函數。另外,我有一個linkedList.cpp和一個linkedList.h,它們都工作100%完美(很多測試),所以我不相信顯示linkedList的文字代碼將是有益的。
我的一些代碼:(裸陪我,這是一個有點混亂)
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
您還需要在構建時鏈接鏈接列表對象文件。 –
就像在linkedList.o文件中一樣?我會怎麼做?我知道我包含了linkedList類的頭文件。 – Ibrahim