gpt4 book ai didi

c++ - 使用二叉搜索树定义运算符 == 和运算符 > 时出错

转载 作者:行者123 更新时间:2023-11-28 08:13:57 25 4
gpt4 key购买 nike

我正在开发一个程序,该程序读取用户输入的文本文件,创建用户输入的文本文件,命名用户想要的文本文件,然后对文本文件进行排序,在用户输入的上方排序单词在阈值中并显示单词及其在用户指定的输出文件中找到的次数。我已经完成了大部分代码,但是我得到了一个编译器错误,这里是示例输出、错误和代码

示例输出

输入命令文件名;按返回。历史.in输入输出文件的名称;按返回。历史.out输入测试运行的名称;按返回。样本输入要考虑的最小字号。5个示例结果(在用户指定的输出文件中找到):样本算盘 4摘要 1加 1加法 2预付款 13 之后

其中单词是在文本文件中找到的单词,旁边的数字是找到的次数。

编译错误是:

C:\Users\kevin jack\Desktop\prog-4>g++ -o try main.cpp
main.cpp:(.text+0x329) undefined reference to `StrType::PrintToFile(bool, std::basic_ofstream<char, std::char_traits<char> >&)'
:main.cpp:(.text+0x608): undefined reference to `StrType::GetStringFile(bool, InType, std::basic_ifstream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x639): undefined reference to `StrType::LenghtIs()'
main.cpp:(.text+0x6d8): undefined reference to `StrType::GetStringFile(bool, InType, std::basic_ifstream<char, std::char_traits<char> >&)'
collect2: ld returned 1 exit status

我不知道这是什么意思如果有人知道请告诉我这里是我的代码

主要.cpp

//main.cpp
#include <fstream>
#include "StrType.h"
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

struct WordType
{
public:
StrType word;
int count;


};


struct TreeNode
{
WordType info;
TreeNode* left;
TreeNode* right;

};

class ListType
{
public:
ListType();
void InsertOrIncrement (StrType string);
void Print(std::ofstream&) const;
private:
TreeNode* root;
};


ListType::ListType()
{
root=NULL;
}

void Process(TreeNode*& tree, StrType s)
{

if(tree == NULL)
{
tree = new TreeNode;
tree->info.word = s;
tree->info.count = 1;
tree->left = NULL;
tree->right = NULL;
}

else if (tree->info.word == s)
tree->info.count++;
else if (s < tree->info.word)
Process(tree->left, s);
else
Process(tree->right, s);
}

void ListType::InsertOrIncrement(StrType s)
{
Process(root, s);
}

void Print (TreeNode* tree, std::ofstream& outFile)
{

if (tree!= NULL)
{
Print(tree->left, outFile);
tree->info.word.PrintToFile(true, outFile);
outFile <<" "<< tree->info.count;
Print(tree->right, outFile);
}
}

void ListType::Print(std::ofstream& outFile) const
{
::Print(root, outFile);
}


int main()
{
using namespace std;
ListType list;
string inFileName;
string outFileName;
string outputLabel;
ifstream inFile;
ofstream outFile;
StrType string;
int minimumLenght;

cout<<"enter in imput file name."<<endl;
cin>>inFileName;
inFile.open(inFileName.c_str());

cout<<"enter name of output file."<<endl;
cin>>outFileName;
outFile.open(outFileName.c_str());

cout<<"enter name of test run."<<endl;
cin>>outputLabel;
outFile<< outputLabel << endl;

cout<<"enter the min word size."<<endl;
cin>>minimumLenght;

string.GetStringFile(true, ALPHA_NUM, inFile);
while(inFile)
{

if(string.LenghtIs() >= minimumLenght)
list.InsertOrIncrement(string);
string.GetStringFile(true, ALPHA_NUM, inFile);
}

list.Print(outFile);
outFile.close();
inFile.close();
return 0;
}

字符串类型.h

//StrType.h
#include <fstream>
#include <iostream>

const int MAX_CHARS=100;
enum InType{ALPHA_NUM, ALPHA, NON_WHITE, NOT_NEW};

class StrType
{
public:
void MakeEmpty();
void GetString(bool skip, InType charsAllowed);
void GetStringFile(bool skip, InType charsAllowed,
std::ifstream& inFile);
void PrintToScreen(bool newLine);
void PrintToFile(bool newLine, std::ofstream& outFile);
int LenghtIs();
void CopyString(StrType& newString);
bool operator==(const StrType& other) const;
bool operator<(const StrType& other) const;




private:
char letters[MAX_CHARS + 1];

};
bool StrType::operator==(const StrType& other) const
{
return (strcmp(letters, other.letters) == 0);
}

bool StrType::operator<(const StrType& other) const
{
return (strcmp(letters, other.letters) < 0);
}

void StrType::MakeEmpty()
{
letters[0] ='\0';
}

我试图重载 == 和 > 运算符。我在 StrType 类中声明它并在它下面定义它,但我不确定我是否正确定义它,甚至在正确的位置!任何帮助将不胜感激

最佳答案

如果你想编译和检查你的代码,你应该至少将所有类的所有成员函数定义为(几乎)空函数。

void StrType::GetString(bool skip, InType charsAllowed)  
{
// empty function, you will write your code heree later
}
// ...

不要忘记非空函数的返回值

关于c++ - 使用二叉搜索树定义运算符 == 和运算符 > 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com