1

我是 C++ 编程新手。实现堆栈也是新的。我的目标是使用模板堆栈创建 RPN 计算器。不能使用内置的堆栈类。

到目前为止我拥有一切,现在我被卡住了,我想不出如何解决这个问题。我目前收到这些错误:

Error   C2109   subscript requires array or pointer type
Warning C4244   'return': conversion from 'double' to 'int', possible loss of data

这是我的堆栈类:

    #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模板类中的方法。

4

1 回答 1

0

看起来您有一个函数参数,该参数与类成员(您的存储)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_, where datawould become m_data。随意使用任何你想要的代码风格,但我建议尽可能避免冲突(和第二种方法)。

于 2017-08-19T23:20:16.917 回答