C ++将值添加到堆栈下标需要数组或指针类型和警告

时间:2017-08-19 23:00:37

标签: c++ visual-studio-2010 templates stack que

我是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方法。

1 个答案:

答案 0 :(得分: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_作为前缀,其中data将成为m_data。随意使用您想要的任何类型的代码,但我建议尽可能避免冲突(和第二种方法)。