C++将值添加到堆栈下标需要数组或指针类型和警告
我不熟悉C++编程。对于实现堆栈也是新的。我的目标是使用模板堆栈创建RPN计算器。不能使用内置的堆栈类。C++将值添加到堆栈下标需要数组或指针类型和警告
我有一切到目前为止,现在我卡住了,我想不出如何解决这个问题。我目前收到这些错误:
Error C2109 subscript requires array or pointer type
Warning C4244 'return': conversion from 'double' to 'int', possible loss of data
这是我的Stack类:
#include<stack>
#define STACK_MAX 500
template<class T>
class RPNCalculator
{
private:
//Insanciating stack class
T data[STACK_MAX];
int size;
//stack<T> rpnstack;
public:
RPNCalculator() {
size = 0;
}
~RPNCalculator();
int Top() {
if (size == 0) {
fprintf(stderr, "Error: stack empty\n");
return -1;
}
return data[size - 1];
}
void push(T data); // pushes a new operand onto the stack
// the following operations are to be performed as defined for Reverse Polish Notation
// binary operators:
T value(); // returns the topmost value
void pop(); // returns the topmost value and pops it off the top
double add();
double subtract();
double multiply();
double divide();
// unary operators:
double square(); // squares the current value
double negate(); // negates, i.e. 3 becomes -3
bool isEmpty(); // tests to see if there are elements on the stack
void clear(); // clears out the stack
};
template<class T>
inline bool RPNCalculator<T>::isEmpty()
{
bool status;
if (!top)
status = true;
else
status = false;
return status;
}
template<class T>
void RPNCalculator<T>::clear()
{
}
template<class T>
inline RPNCalculator<T>::~RPNCalculator()
{
}
template<class T>
inline void RPNCalculator<T>::push(T data)
{
if (size < STACK_MAX)
data[size++] = data;
else
fprintf(stderr, "Error: stack full\n");
}
template<class T>
inline T RPNCalculator<T>::value()
{
return T();
}
template<class T>
inline void RPNCalculator<T>::pop()
{
if (size == 0)
fprintf(stderr, "Error: stack empty\n");
else
size--;
}
这是我的主类:
#include <iostream>
#include "RPNCalculator.h"
#include <string>
#include <sstream>
using namespace std;
bool isOperator(const string& input);
void performOperation(const string& st, RPNCalculator<double>& rpnstack);
int main() {
cout << "Welcome to the RPN Calculator by AbdulFatai Saliu __D00168401" << endl;
cout << "Enter c to clear \n"
<< "s to square \n"
<< "n to negate \n"
<< "p to pop current value \n"
<< "q to quit \n"
;
RPNCalculator<double> rnpstack;
string input;
while (true) {
//Dispaly prompt
cout << ">> ";
//get user input
cin >> input;
//check for numeric values
double numereric;
if (istringstream(input) >> numereric) {
}
else if (isOperator(input)) {
}
else if (input == "q") {
return 0;
}
else {
cout << "Input Not Valid" << endl;
}
//check for operators
//check for exit
// display invalid value message
}
system("PAUSE");
//return 0;
}
bool isOperator(const string& input) {
string operators[] = { "-","+","*","/"};
for (int i = 0; i < 6; i++) {
if (input == operators[i]) {
return true;
}
}
return false;
}
void performOperation(const string& input, RPNCalculator<double>& rpnstack) {
double firstValue, secondValue, result;
firstValue = rpnstack.Top();
rpnstack.pop();
secondValue = rpnstack.Top();
rpnstack.pop();
if (input == "-") {
result = secondValue - firstValue;
}
else if (input == "+") {
result = secondValue + firstValue;
}
else if (input == "*") {
result = secondValue * firstValue;
}
else if (input == "/") {
result = secondValue/firstValue;
}
cout << result << endl;
rpnstack.push(result);
}
的问题似乎从我的push()
方法来在RPNCalculator
模板类。
看起来您有一个参数,其功能void push(T data);
其中参数与类成员(data
,您的存储)名称相同。尝试更改函数实现中不会产生此冲突的参数名称。如果您真的想使用该名称,您也可以指定使用哪个data
。
试试这一个,而不是
template<class T>
inline void RPNCalculator<T>::push(T arg)
{
if (size < STACK_MAX)
data[size++] = arg;
else
fprintf(stderr, "Error: stack full\n");
}
,或者,如果你想明确了解哪些数据您通过的方式,其中命名成员变量有分配
template<class T>
inline void RPNCalculator<T>::push(T data)
{
if (size < STACK_MAX)
this->data[size++] = data; // this->data is the member, data is the function local variable
else
fprintf(stderr, "Error: stack full\n");
}
这通常是可以避免的不能冲突。一种方法是在m_
前加上您的成员,其中data
将变为m_data
。随意使用任何你想要的代码样式,但是我建议尽可能避免冲突(和第二种方法)。
谢谢你的问题,你也有什么想法为什么当我尝试在栈中打印值时没有打印任何东西? – Destructor2017
@ Destructor2017我看不到任何打印代码你可以发布一个关于代码的新问题以及你有什么问题吗?请随时在这里链接这个问题。 – N00byEdge
嘿,谢谢,我想到了这个问题。 – Destructor2017
'data [size ++] = data;'这里'data'的两个实例都是指名为'data'的函数参数,而不是名为'data'的类成员。你似乎期望编译器读取你的想法,并在任何特定时刻神奇地猜测你的意思。不要混淆你自己,给不同的名字不同的东西。 –
所以有什么改变? ( – Destructor2017
我无法解析这个问题 –