| VelikiDrMr (3) | |||||
|
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:
And here's the class definition in case it's helpful:
Thanks in advance for any help. | |||||
|
|
|||||
| Chervil (1205) | |
Try ulaz >> pomz;, instead of <<
| |
|
|
|
| VelikiDrMr (3) | |
|
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? | |
|
|
|
| Chervil (1205) | |
Did you ever use cin or cout? Which operator did you use in each case, << or >>, and why?
| |
|
|
|
| Cubbi (1927) | |||
<< is the output operator, ifstream is an input-only stream. You can't output into an input.
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
|
|||
| VelikiDrMr (3) | ||
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. | ||
|
|
||