[英]binary search tree overloading == operator or just need general advice
我正在研究一個程序,該程序讀取用戶輸入的文本文件,創建用戶輸入的文本文件,命名用戶所需的文本文件,然后對文本文件進行排序,並在閾值和在用戶指定的輸出文件中顯示單詞以及找到該單詞的次數。 我已經完成了大部分代碼,但是我得到了編譯器錯誤,這里是示例輸出,錯誤和代碼
樣本輸出
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';
}
我認為這與重載==運算符有關,但我不太擅長重載。 如果有人可以幫助我,那將是很棒的。
您不是要重載,而是要為class StrType
定義 operator==
和operator<
,您可以按照以下步驟進行操作:
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);
}
您需要為結構WordType實現“ operator ==”和“ operator <”。
它們可以是這樣的成員函數:
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.