我正在开发一个程序,该程序读入用户输入的文本文件,创建用户输入的文本文件,命名用户想要的文本文件,然后对输入的用户上方的文本文件排序单词进行排序在阈值中,显示用户指定的输出文件中的单词和次数。我已经完成了大部分代码,但是我得到了一个编译器错误,下面是示例输出,错误和代码
示例输出
Enter name of input command file; press return.
history.in
Enter name of output file; press return.
history.out
Enter name of test run; press return.
sample
Enter the minimum size word to be considered.
5
Sample results (found in user specified output file):
sample
abacus 4
abstract 1
adding 1
addition 2
advances 1
after 3
其中单词是在文本文件中找到的单词,旁边的数字是找到它的次数。
错误代码
C:\Users\kevin jack\Desktop\prog-4>g++ -o try main.cpp
main.cpp: In function `void Process(TreeNode*&, StrType)':
main.cpp:49: error: no match for 'operator==' in 'tree->TreeNode::info.WordType:
:word == s'
main.cpp:51: error: no match for 'operator<' in 's < tree->TreeNode::info.WordTy
pe::word'
的main.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;
}
StrType.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);
private:
char letters[MAX_CHARS + 1];
};
void StrType::MakeEmpty()
{
letters[0] ='\0';
}
我认为这是关于重载==运算符,但我不是很擅长重载。如果有人可以帮助我,那么可能会很棒。
答案 0 :(得分:1)
您不是超载,而是定义 operator==
和operator<
作为class StrType
你可以这样做:
class StrType
{
public:
...
bool operator==(const StrType& other) const;
bool operator<(const StrType& other) const;
...
};
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);
}
答案 1 :(得分:0)
您需要实现“operator ==”和“operator&lt;” for struct WordType。
它们可以是这样的成员函数:
struct WordType
{
bool operator == (const WordType &other)
{
// implementation
}
bool operator < (const WordType &other)
{
// implementation
}
};
或者像这样的全球函数:
bool operator == (const WordType &left, const WordType &right)
{
// implementation
}
bool operator < (const WordType &left, const WordType &right)
{
// implementation
}