2015-11-20 69 views
0

我无法确定如何解决此问题未定义的引用错误。我有类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 
+0

您还需要在构建时链接链接列表对象文件。 –

+0

就像在linkedList.o文件中一样?我会怎么做?我知道我包含了linkedList类的头文件。 – Ibrahim

回答

0

想通了:

1)你可以做一个makefile;例如,我使用PuTTY来完成此操作。

2)因为我们希望makefile是“无扩展”的,所以在Unix中生成makefile是理想的。

3)这里是你如何把它写在此情况下(腻子,用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后,你只需在目录中输入'make'来运行它。然后你会看到你的标签(在makefile中使用的标签)和makefile在同一个目录下。那么你只需键入这个运行“的所有功能于一身的”方案:./Label

- 编辑 -

如果你不想做一个makefile,你可以defenitely只需键入腻子以下内容:

g++ -o HT hashtable.cpp linkedList.cpp main.cpp [PRESS ENTER] 

但是,每次更改代码而不是makefile时,您都必须输入此值。

+1

你没有*有*有一个makefile(虽然这是一个有用的想法),你可以写'g ++ -o HT hashtable.cpp linkedList.cpp main.cpp' –

+0

@ M.M谢谢!我完全忘记了这一点,因为我有一段时间没有使用PuTTY。编辑。 – Ibrahim

相关问题