C++ 前缀计算器在编译时遇到问题
我在一个程序中遇到了一些问题,该程序应该采用命令行表达式并将其解释为正常的数学表达式。这就是我得到的错误: driver.cpp:在函数“int mainit,char**”中: driver.cpp:17:57:错误:调用'PrefixCalculator::evalstd::istringstream'没有匹配的函数 司机:cpp:17:57:注:候选人为: PrefixCalculator.h:33:3:note:T PrefixCalculator::evalstd::istringstream&[with T=int,std::istringstream=std::basic_istringstream] PrefixCalculator.h:33:3:注意:参数1从'std::istringstream{aka std::basic_istringstream}'到'std::istringstream&{aka std::basic_istringstream&}'没有已知的转换 我不明白这个错误试图向我暗示什么。 有没有解决这个问题的建议?稍后我将添加异常,因此它们现在将被注释掉 代码如下: PrefixCalculator.cppC++ 前缀计算器在编译时遇到问题,c++,calculator,prefix,C++,Calculator,Prefix,我在一个程序中遇到了一些问题,该程序应该采用命令行表达式并将其解释为正常的数学表达式。这就是我得到的错误: driver.cpp:在函数“int mainit,char**”中: driver.cpp:17:57:错误:调用'PrefixCalculator::evalstd::istringstream'没有匹配的函数 司机:cpp:17:57:注:候选人为: PrefixCalculator.h:33:3:note:T PrefixCalculator::evalstd::istringst
#pragma once
#include <sstream>
using namespace std;
template<class T>
class PrefixCalculator {
public:
PrefixCalculator(void){
numOperator = 0;
numOperand = 0;
};
~PrefixCalculator(void){};
T eval(istringstream&);
int getNumOperator() {
return numOperator;
};
int getNumOperand() {
return numOperand;
};
private:
//if you feel you need private helper functions and/or helper data
int numOperator;
int numOperand;
};
template<class T>
T PrefixCalculator<T>::eval(istringstream& input) {
//this function needs to throw an exception if there's a problem with the expression or operators
char nextChar = input.peek();
//this while loop skips over the spaces in the expression, if there are any
while(nextChar == ' ') {
input.get(); //move past this space
nextChar = input.peek(); //check the next character
}
if(nextChar == '+') {
input.get(); //moves past the +
numOperator++;
return eval(input) + eval(input); //recursively calculates the first expression, and adds it to the second expression, returning the result
}
/***** more operators here ******/
if(nextChar == '-') {
input.get();
numOperator++;
return eval(input) - eval(input);
}
if(nextChar == '*') {
input.get();
numOperator++;
return eval(input) * eval(input);
}
if(nextChar == '/') {
input.get();
numOperator++;
return eval(input) / eval(input);
}
/****** BASE CASE HERE *******/
//it's not an operator, and it's not a space, so you must be reading an actual value (like '3' in "+ 3 6". Use the >> operator of istringstream to pull in a T value!
input>>nextChar;
T digit = nextChar - '0';
numOperand++;
return digit;
//OR...there's bad input, in which case the reading would fail and you should throw an 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 a prefix expression to evaluate, or q to quit." << endl;
getline(cin,expression);
while(expression[0] != 'q') {
//try {
int result = calc.eval(istringstream(expression));
cout << result << endl;
//}
//catch { //will not compile, you have to finish this!
//
//}
cout << "Give a prefix expression to evaluate or q to quit." << endl;
getline(cin,expression);
}
return 0;
}
计算evalistringstreamexpression;将一个临时实例传递给eval,则需要一个左值。
为流提供一个额外的变量
istringstream iss(expression);
把那个递给我
int result = calc.eval(iss);