ifstream has no match for operator << with char

Hello, I'm writing a program that is meant to simulate a Turing machine, and I'm currently stuck at simulating the tape. I actually think I've got it all done, but there seems to be something I missed when reading from a textual files. I've decided that the contents of the tape will be read from a textual data file, but I'm getting an error when I use ulaz<<pomz, where ulaz is of type ifstream, and pomz is a char. I first tried impleteing it in C style, with a FILE* and using fscanf, but decided that I should learn how to use the C++ variant.

The error I'm getting is this:
"error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &&,_Ty)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &&' from 'std::ifstream'"

This error appears about 100 times, on lines 13 and 21, which are ulaz << pomz.

There is also another error, which is:
"error C2676: binary '<<' : 'std::ifstream' does not define this operator or a conversion to a type acceptable to the predefined operator"

This one appears on line 13.

Here's the code of the troublesome method:

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
#include <iostream>
#include <fstream>
using namespace std;

#include "traka.h"

void Traka::ucitajTraku(char* imeDat) {
        ifstream ulaz;
        ulaz.open(imeDat,ios::in);
        char pomz;
	
        int i = 0;
        ulaz << pomz;
        while (pomz != EOF)
                i++;
        ulaz.close();

        this->mem = new char[i+1];
        ulaz.open(imeDat,ios::in);
        for (int j = 0; j < i; j++) {
                ulaz << pomz;
                if (pomz == '\n') {
                        j--;
                        i--;
                }
                else
                        this->mem[j] = pomz;
        }
        this->mem[i] = '\0';

        this->brslova = strlen(mem);
        ulaz.close();
}


And here's the class definition in case it's helpful:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Traka {
	char *mem;
	int brslova;

	Traka(const Traka& t) {}
	Traka& operator=(const Traka& t) {}
public:
	Traka() {}

	void ucitajTraku(char* imeDat);
	void pisi(ostream& os, int pozglave);
	char operator[](int i) const;
	char& vratiRef(int i);

	~Traka() {
		delete mem;
	}
};


Thanks in advance for any help.
Try ulaz >> pomz;, instead of <<
Even though that makes no sense, since I'm trying to read, I did already try it, and no changes.

I did however fix the issue by using

ulaz.get(pomz),

and it seems to be working.
I also discovered that the

while (pomz != EOF)

doesn't work, and I changed that to

while(ulaz.get(pomz)).

While the problem is solved now, can someone explain why I can't use << with ifstream? And why get doesn't read the EOF sign?
Did you ever use cin or cout? Which operator did you use in each case, << or >>, and why?
why I can't use << with ifstream?

<< is the output operator, ifstream is an input-only stream. You can't output into an input.

And why get doesn't read the EOF sign?

if you want to compare something to EOF, it would be while( (pomz = ulaz.get()) != EOF) (which is the way such loops are done in C), but you first have to change pomz from char to int. EOF cannot be stored in a char variable.
Last edited on
<< is the output operator, ifstream is an input-only stream. You can't output into an input.


Yes, that would be a problem. My concentration is really low today.

I solved it by using get() and getline() methods and it works now.

Thanks for the explanation.
Topic archived. No new replies allowed.