二叉搜索树重载==操作符或只需要一般建议



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

的示例输出
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:Userskevin jackDesktopprog-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] ='';
}

我认为这是关于重载==操作符,但我不是很擅长重载。如果有人能帮我一把,那就太好了。

您不能重载,而是为您的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);  
}  

您需要为struct WordType实现"operator =="one_answers"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
}

最新更新