Thursday, 15 July 2010

c++ - prefix calculator having trouble compilng -



c++ - prefix calculator having trouble compilng -

i having problem programme supposed take command line look , interpret normal mathematical expression.this getting error:

driver.cpp: in function ‘int main(int, char**)’: driver.cpp:17:57: error: no matching function phone call to‘prefixcalculator::eval(std::istringstream)’

driver.cpp:17:57: note: candidate is: prefixcalculator.h:33:3: note: t prefixcalculator::eval(std::istringstream&) [with t = int, std::istringstream = std::basic_istringstream]

prefixcalculator.h:33:3: note: no known conversion argument 1 ‘std::istringstream {aka std::basic_istringstream}’ ‘std::istringstream& {aka std::basic_istringstream&}’

i can't understand error trying suggest me. suggestions fixing that? i'm going add together exceptions later, commented out now.

this code:

prefixcalculator.cpp

#pragma 1 time #include <sstream> using namespace std; template<class t> class prefixcalculator { public: prefixcalculator(void){ numoperator = 0; numoperand = 0; }; ~prefixcalculator(void){}; t eval(istringstream&); int getnumoperator() { homecoming numoperator; }; int getnumoperand() { homecoming numoperand; }; private: //if sense need private helper functions and/or helper info int numoperator; int numoperand; }; template<class t> t prefixcalculator<t>::eval(istringstream& input) { //this function needs throw exception if there's problem look or operators char nextchar = input.peek(); //this while loop skips on spaces in expression, if there while(nextchar == ' ') { input.get(); //move past space nextchar = input.peek(); //check next character } if(nextchar == '+') { input.get(); //moves past + numoperator++; homecoming eval(input) + eval(input); //recursively calculates first expression, , adds sec expression, returning result } /***** more operators here ******/ if(nextchar == '-') { input.get(); numoperator++; homecoming eval(input) - eval(input); } if(nextchar == '*') { input.get(); numoperator++; homecoming eval(input) * eval(input); } if(nextchar == '/') { input.get(); numoperator++; homecoming eval(input) / eval(input); } /****** base of operations case here *******/ //it's not operator, , it's not space, must reading actual value (like '3' in "+ 3 6". utilize >> operator of istringstream pull in t value! input>>nextchar; t digit = nextchar - '0'; numoperand++; homecoming digit; //or...there's bad input, in case reading fail , should throw exception }

driver.cpp

#include <sstream> #include <string> #include <iostream> #include "prefixcalculator.h" using namespace std; int main(int argc, char** argv) { prefixcalculator<int> calc; string expression; cout << "give prefix look evaluate, or q quit." << endl; getline(cin,expression); while(expression[0] != 'q') { //try { int result = calc.eval(istringstream(expression)); cout << result << endl; //} //catch { //will not compile, have finish this! // //} cout << "give prefix look evaluate or q quit." << endl; getline(cin,expression); } homecoming 0; }

calc.eval(istringstream(expression)); passes temporary instance eval(), you'll need lvalue. provide variable stream

istringstream iss(expression);

and pass 1

int result = calc.eval(iss);

c++ calculator prefix

No comments:

Post a Comment