hello all! i am working on a program that reads in a text file that the user inputs, creates a text file that the user inputs, names the text file that the user wants, and then sorts the text file sorting words above the user entered in threshold and displays the words and how many times it was found to the output file the user specify's. i have most of the code finished but im getting a compiler error heres the sample output, error and code

sample output

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

where the word is the word found in the text file, and the number next to it is how many times it was found.

the error code

C:\Users\kevin jack\Desktop\prog-4>g++ -o test test.cpp
test.cpp: In function `void Process(TreeNode*&, StrType)':
test.cpp:48: error: no match for 'operator==' in 'tree->TreeNode::info.WordType:
:word == string'
test.cpp:50: error: no match for 'operator<' in 'string < tree >TreeNode::info.WordType::word'
test.cpp:51: error: 'struct TreeNode' has no member named 'eft'
test.cpp: In member function `void ListType::Print(std::ofstream&) const':
test.cpp:74: error: no matching function for call to `ListType::Print(TreeNode*
const&, std::basic_ofstream<char, std::char_traits<char> >&) const'
test.cpp:73: note: candidates are: void ListType::Print(std::ofstream&) const

and the code

//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 string)
{
     if(tree == NULL)
     {
         tree = new TreeNode;
         tree->info.word = string;
         tree->info.count = 1;
         tree->left = NULL;
         tree->right = NULL;
     }
     else if (tree->info.word == string)
         tree->info.count++;
     else if (string < tree->info.word)
         Process(tree->eft, string);
     else 
         Process(tree->right, string);
}

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

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
#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';
}

if anyone could give me advice as to what im doing wrong or help me i would be tremendously grateful

thanks kevin

Recommended Answers

All 10 Replies

Well, for starters you have some goofiness in your code such as using string as a variable name when it's also a type and trying to call a global function from a member function of the same name without qualification. Here's the main.cpp with those problems fixed because they're a bit more subtle:

//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;
}

Now you can focus on the two remaining errors:

else if (tree->info.word == s)

error: no match for 'operator==' in 'tree->TreeNode::info.WordType::word == s'

This error states that the WordType class does not support comparisons against the StrType class with the == operator. This doesn't happen magically, you need to define it.

else if (s < tree->info.word)

error: no match for 'operator<' in 's < tree >TreeNode::info.WordType::word'

Once again, this operation isn't magically supported, you need to define it to work as intended.

how should i define it? in the .h file? ive tried moving the struct wordtype to the .h and its still giving me an error. maybe if you could explain in pseudo what i need to do or give me an example so i can better understand what your saying

thanks kevin

maybe if you could explain in pseudo what i need to do or give me an example so i can better understand what your saying

You need to write an overloaded operator==() and operator<() in your WordType class that compares the string member. Since the StrType class is also a custom class without those operations, you'll want to do the same thing there as well, where the operators perform variations of strcmp().

i am still confused on how to overload my operator. sorry i am not very good at the overloading process.

what i have so far is

struct WordType
{
       public:
              StrType word;       
              int count;
              ostream& operator==const WordType;
              int a = word.lenght();
              int b = tree->word.lenght();
};

but im not sure if im on the right path guidance would be appreciated

thanks kevin

or would it be

friend istream & operator>> ( istream is, TreeNode* &s);
friend ostream & operator<< ( ostream os, const TreeNode* &s );

i just dont understand how to overload it that well

thanks kevin

i think it would be like this but im not sure if its write or what more i have to do. any advice would be most helpful

struct WordType
{
       public:
              StrType word;       
              int count;
              bool operator==(const WordType& first, const WordType& second);
              bool operator<(const WordType& first, const WordType& second);
};

    bool WordType::operator==(const WordType& first, const WordType& second)
   {
   }

    bool WordType::operator<(const WordType& first, const WordType& second)
    {
    }

Change StrType to this:

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

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& rhs) const
    {
        return strcmp(letters, rhs.letters) == 0;
    }
    bool operator<(const StrType& rhs) const
    {
        return strcmp(letters, rhs.letters) < 0;
    }
private:
    char letters[MAX_CHARS + 1];
};

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

thank you so much for your help!! when i try to compile it all i get this error

error code

C:\Users\kevin jack\Desktop\11-23>g++ -o prog4 main.cpp
/cygdrive/c/Users/KEVINJ~1/AppData/Local/Temp/ccOpqSSW.o:main.cpp:(.text+0x2dd):
undefined reference to `StrType::PrintToFile(bool, std::basic_ofstream<char, st
d::char_traits<char> >&)'
/cygdrive/c/Users/KEVINJ~1/AppData/Local/Temp/ccOpqSSW.o:main.cpp:(.text+0x5bc):
undefined reference to `StrType::GetStringFile(bool, InType, std::basic_ifstrea
m<char, std::char_traits<char> >&)'
/cygdrive/c/Users/KEVINJ~1/AppData/Local/Temp/ccOpqSSW.o:main.cpp:(.text+0x5ed):
undefined reference to `StrType::LenghtIs()'
/cygdrive/c/Users/KEVINJ~1/AppData/Local/Temp/ccOpqSSW.o:main.cpp:(.text+0x68c):
undefined reference to `StrType::GetStringFile(bool, InType, std::basic_ifstrea
m<char, std::char_traits<char> >&)'
collect2: ld returned 1 exit status

do you know how i can correct this?

do you know how i can correct this?

I suspect ditching this program and starting with something smaller would be a good start, because you clearly don't have enough experience writing C++ yet. One of the most basic concepts in C++ is the distinction between a function declaration and a function definition. Without a definition (that little block of code between curly braces), you'll get undefined reference errors.

ok i think that is probably the best plan then. thank you for all of your help though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.