Error: No match for 'operator<<'

I was assigned a program where the main calls a Word object and the Word object counts the vowels, consonants, digits, and special characters in the word. I've managed to do everything using cout, but when they ask me to use ofstream instead of ostream, I get errors that I don't know how to fix.

Here's a couple chunks of code from my main and my Word methods ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){
     //initialization of everything
     ostream writeout("output.txt");
     //Word array filled out reading from input txt file
     //Close input file
     for(int i = 0; i < count; i++){
          words[i].write(writeout);
          writeout << endl;
     }
     writeout.close();
     return 0;
}

void Word::write(ofstream &writeout) const{
     writeout << setw(12) << left << words;
     writeout << setw(8) << right << vowels;
     writeout << setw(8) << right << consonants;
     writeout << setw(8) << right << digits;
     writeout << setw(8) << right << specialchars;
}


and I end up with the following errors:

Word.cpp:32: error: no match for 'operator<<' in 'writeout << std::setw(12) '
Word.cpp:33: error: no match for 'operator<<' in 'writeout << std::setw(8) '
Word.cpp:34: error: no match for 'operator<<' in 'writeout << std::setw(8) '
Word.cpp:35: error: no match for 'operator<<' in 'writeout << std::setw(8) '
Word.cpp:36: error: no match for 'operator<<' in 'writeout << std::setw(8) '

Any help would be greatly appreciated =)
woops..
Last edited on
You haven't declared most of the stuff in your code...
I'm pretty sure you can't use setw and such with fstreams.
All of my code works except for the parts I posted above. For the complete code, it looks like this:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "Word.h"

using namespace std;

const int MAX = 100;

int main()
{
     ifstream input("words.txt");
     ofstream writeout("output.txt");
     string inword;
     Word words[MAX];
     int count = 0;
     while(count < MAX && input >> inword)
     {
          words[count].setWord(inword);
          count++;
     }
     input.close();
     writeout << setw(12) << left << "Word";
     writeout << setw(8) << right < "Vowels";
     writeout << setw(8) << right < "Const.";
     writeout << setw(8) << right < "Digits";
     writeout << setw(8) << right < "Special";
     writeout << endl;
     for(int i = 0; i < count; i++)
     {
          words[i].write(writeout);
          writeout << endl;
     }
     writeout.close();
     return 0;
}


Word.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef _WORD_H_
#define _WORD_H_

#include <cstring>
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

class Word
{
     public:
          Word();
          ~Word();
          void setWord(const string input);
          void write(ofstream &writeout) const;
     private:
          string words;
          int vowels;
          int consonants;
          int digits;
          int specialchars;
};

#endif 


Word.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "Word.h"
#include <ctype.h>

Word::Word(){
     words = string();
     vowels = 0;
     consonants = 0;
     digits = 0;
     specialchars = 0;
}
Word::Word(){
     cout << "The 'apple' object is being destroyed!" << endl;
}
void Word::setWord(const string input){
     words = input;
     for(int c = 0; c < input.length(); c++){
          if(tolower(words[c]) == 97 || tolower(words[c]) == 101 || tolower(words[c]) == 105 || tolower(words[c]) == 111 || tolower(words[c]) == 117){
          vowels++;
          }
          else if(isalpha(words[c]) != 0){
               consonants++;
          }
          else if(isdigit(words[c] != 0){
               digits++;
          }
          else{
               specialchars++;
          }
     }
}
void Word::write(ofstream &writeout) const{
     writeout << setw(12) << left << words;
     writeout << setw(8) << right << vowels;
     writeout << setw(8) << right << consonants;
     writeout << setw(8) << right << digits;
     writeout << setw(8) << right << specialchars;
}
I don't see anywhere the necessary #include <fstream> in Word.h.
Thank you so much webJose! Everything compiled and ran perfectly after that. =)
Topic archived. No new replies allowed.