从另一个对象调用一个对象的函数时未定义的引用

Undefined Reference when Invoking Function of One Object from Another Object

我无法弄清楚如何解决此 未定义引用 错误。我有 class hashTable 并且在它的 insertWord(string) 函数 定义 中,我调用了向量中的 linkedList 对象的函数。问题是我得到这个错误:

错误:

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 class 中的 linkedList instances/functions 的范围有关。另外,我有一个 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

想通了:

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

2) 因为我们希望 makefile 是 "extension-less",所以在 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 之后,您只需 运行 通过在目录中键入 'make' 即可。然后您将在与 makefile 相同的目录中看到您的标签(在您的 makefile 中使用的标签)。然后你只需输入 运行 你的 "all-in-one" 程序:./Label

--编辑--

如果您不想制作 makefile,您完全可以在 PuTTY 中输入以下内容:

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

但是,与 makefile 相比,每次更改代码时都必须键入此内容。