从另一个对象调用一个对象的函数时未定义的引用
Undefined Reference when Invoking Function of One Object from Another Object
我很难弄清楚如何修复这个未定义引用错误。我有类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相反,每次修改代码时都必须键入此项。
相关文章:
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- 为什么销毁被放置 new 覆盖的对象不是未定义的行为?
- 在VSCode中编译C++给了我一个未定义的参考
- 未定义的对象(〔basic.life〕/8):为什么允许引用重新绑定(和常量修改)
- 访问从联合与另一个成员集复制的联合中的一个成员是否未定义或未指定?
- 正在通过const-ref未定义的行为捕获新构造的对象
- 为什么T是未定义的?我正在尝试实现一个用于双链表的节点类,它不喜欢我使用友元运算符后的T
- 在另一个文件中初始化的 extern 数组中未定义的引用
- 我对C++程序有一个未定义的参考,不知道如何处理它
- 我正在尝试在我的类中创建一个静态成员,但编译器警告我它是未定义的
- 对 Class::Class() 和函数在一个文件中的未定义引用? c++
- 仅标头类 + 仅当返回该类的对象时,对函数的未定义引用
- 如果用户尝试从 JS 调用对象的未定义函数C++则回调C++代码
- C++ / G++ Maxmind geolite2++ 第三方共享对象未定义引用
- 通过"this"访问另一个对象的定义?
- 无法加载共享对象:未定义的符号
- 从另一个对象调用一个对象的函数时未定义的引用
- 模板和静态对象(未定义的引用和必需的引用)
- 调用在另一个对象内部定义的链表对象的函数