| luffydc (1) | |||
|
Hello! I'm trying to write a program that will simulate a calculator by using switches that call functions to perform the desired task. When I try compiling I get error messages "error: no match for 'operator>>' in 'infile.std::basic_ifstream<char>::<enonymous.std..." Any help would be appreciated!
| |||
|
|
|||
| Chervil (1206) | |
I don't think you need endl on all your input statements.
| |
|
|
|
| Zaita (2400) | |
|
I would try to simplify you solution significantly. @Chervil is right as well, the endl is not necessary. You should look at using std::getline() as it's not prone to errors like the >> operator. Some points of interest for you. 1. You don't need to pass in the ofstream to every method because it's declared out of the main() method scope. 2. Why are you reading and writing everything from a file and not the console? (cin/cout) 3. You don't need to create a variable to hold the results of all of your methods. e.g return x * x; is better than create a temp variable to hold the result only to return it on the following line (it's only helpful when using a debugger.. but even then marginally) | |
|
|
|
| karakale (14) | ||||||
I agree with Zaita. I think if you delete these lines:
and add these lines on the begining of main() :
you will not get this error. Because you missed opening file and did not call open functions anywhere, so your ifstream types are empty now. | ||||||
|
|
||||||
| IceThatJaw (418) | |
| Another tip. Make the calculator a resusable static class. | |
|
|
|
| Zaita (2400) | |
| There isn't much gained by having it as a static class. Putting the methods in a class and holding an instance of it in the main method would more than suffice. | |
|
|
|
| IceThatJaw (418) | |
|
It makes it easier to use. A simple calculator doesn't need an instance since you are merely using single commands to output and answer. It is basically a utility class. If it was a graphing calculator then an instance would make more sense but even then, when are you ever going to create multiple instances of the calculator? | |
|
Last edited on
|
|
| Zaita (2400) | |
|
But the same argument would be applied to putting them into a class. Why? There is nothing gained from having them as static methods on a class. If you want to isolate the code for a better implementation then put the functions into a namespace, but there is no reason to go OO for the sake of going OO. | |
|
|
|