我的代码给出:
undefined reference to `NgramTree::generateTree(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int)'
collect2.exe: error: ld returned 1 exit status
错误,我不明白为什么。
这是我的代码示例。
NgramTree.cpp
#include "NgramTree.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
void generateTree(string fileName, int n)
{
string line;
string ngram;
bool isWord = 1;
bool firstTime = 1;
ifstream myFile(fileName);
if (!myFile.is_open())
return;
...
NgramTree.h
#include <string>
class NgramTree {
public :
NgramTree (){ };
~NgramTree(){ };
void addNgram (std::string ngram );
int getTotalNgramCount ();
void printNgramFrequencies ();
bool isComplete ();
bool isFull ();
void generateTree(std::string fileName, int n);
};
main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "NgramTree.h"
using namespace std;
int main(){
NgramTree tree;
tree.generateTree("example.txt", 3);
return 0;
}
答案 0 :(得分:0)
您需要在cpp文件中添加类名称。编写此代码,以便编译器知道该方法属于哪个类:
void NgramTree::generateTree(string fileName, int n) {
...