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

Undefined Reference when Invoking Function of One Object from Another Object

本文关键字:一个对象 未定义 引用 函数 调用      更新时间:2023-10-16

我很难弄清楚如何修复这个未定义引用错误。我有类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类中linkedList实例/函数的作用域有关。此外,我有一个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是"无扩展名"的,因此在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"即可运行它。然后,您将在与生成文件相同的目录中看到标签(生成文件中使用的标签)。然后您只需键入以下内容即可运行"一体式"程序:./Label

--编辑--

如果你不想制作一个makefile,你可以在PuTTY中输入以下内容:

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

但是,与makefile相反,每次修改代码时都必须键入此项。

相关文章: