C++创建链栈模板并由此设计一个简单计算器,出现下面两个编译错误请问怎么解决?

  1. 编写了链栈的类模板,保存为 标头.h 文件,并基于此设计了一个简单加减乘除计算器。在编译过程中,出现了下面错误,不知如何解决,求大神指导~

“ 标头.h ” 文件如下:

 //链栈的类模板
#include<cassert>
using namespace std;
template<typename T>class Stack;
template<typename T>class Node{ //链栈结点类模板
    T info;
    Node<T>*link;
public:
    Node(T date = 0, Node<T>*next = NULL) {
        info=data; //编译错误C2563: 在形参表中不匹配
        link=next; //编译错误C2568: “=”无法解析函数重构
    }
    friend class Stack<T>;
};
template<typename T>class Stack{ //链栈类模板,无头结点链表
    Node<T>*top; //栈顶指针
public: 
    Stack() { top = NULL; }
    ~Stack();
    void Push(const T &data); //压栈
    T Pop(); //弹出
    T GetTop();  //取栈顶元素
    void MakeEmpty(); //清空栈
    bool IsEmpty(){ return top == NULL; }
};
template<typename T>Stack<T>::~Stack() { MakeEmpty(); }
template<typename T>void Stack<T>::MakeEmpty() {
    Node<T>*temp;
    while (top != NULL) { temp = top; top = top->link; delete temp; }

}
template<typename T>void Stack<T>::Push(const T &data) {
    top = new Node<T>(data, top);
}
template<typename T>T Stack<T>::Pop() {
    assert(!IsEmpty());
    Node<T>*temp = top;
    T data = temp->info;
    top = top->link; //丢弃栈顶结点
    delete temp; //释放栈顶结点
    return data; //返回栈顶数据
} 
template<typename T>T Stack<T>::GetTop() {
    assert(!IsEmpty());
    return top->info;
}

.cpp文件如下:

#include<iostream>
#include<cmath>
#include<cstdlib>
#include"标头.h" 
using namespace std;
class Calculator { //计算器类
    Stack<int>Nstack;
    Stack<char>Ostack;
public:
    Calculator(void) { };
    void Cal(void);
    void GetTwoNum(int &Num1, int &Num2);
    void Compute(char Opr);
    void Clear(void);
};
void Calculator::Clear() {
    Nstack.MakeEmpty();
    Ostack.MakeEmpty();
}
void Calculator::GetTwoNum(int &Num1, int &Num2) {
    Num1 = Nstack.Pop();
    Num2 = Nstack.Pop();
}
void Calculator::Compute(char Opr) {
    int Num1, Num2;
    if (Opr != '=')GetTwoNum(Num1, Num2);
    switch (Opr) {
    case '+':Nstack.Push(Num2 + Num1); break; //结果压栈
    case '-':Nstack.Push(Num2 - Num1); break;
    case '*':Nstack.Push(Num2 * Num1); break;
    case '/':Nstack.Push(Num2 / Num1); break;
    case '=':cout << Nstack.Pop() << endl;
    }
}
void Calculator::Cal() { //读取用户输入(数字字符or操作字符)
    bool b1 = true,b2 = true;
    char ch1, ch2, str[50]; //ch1存新获取的字符,ch2存从栈中取出的字符
    int k = -1;
    while (b2) {
        cin >> ch1;
        if (ch1 >= '0'&&ch1 <= '9') {
            k++;
            str[k] = ch1;//数字字符加入串中
        }
        else {
            if (k >= 0) {
                str[k + 1] = '\0';
                Nstack.Push(atoi(str)); //数字字符加入数组栈中
                k = -1;
            }
            switch (ch1) {
            case'c':
                Clear();
                break;
            case'+':  
            case'-':                             //+ - 优先级最低,输入新的= —号时,可直接弹出此时栈顶操作符进行计算,并把结果压栈
                while (!Ostack.IsEmpty()) {
                    ch2 = Ostack.Pop();

                    Compute(ch2);
                }
                Ostack.Push(ch1);
                break;
            case'*':
            case'/':
                while (!Ostack.IsEmpty() && b1) {
                    ch2 = Ostack.Pop();    //栈顶运算符出栈
                    if (ch2 == '*' || ch2 == '/') //比较优先级
                        Compute(ch2); //同是 * / 优先级不高,先计算原来栈中已有的* or / 运算符
                    else {
                        Ostack.Push(ch2); //新的优先级高 ,先把原栈中运算符压回去
                        b1 = false;
                    }
                }
                Ostack.Push(ch1); //再把新运算符压栈
                b1 = true;
                break;
            case'=':
                while (!Ostack.IsEmpty()) {
                    ch2 = Ostack.Pop();
                    Compute(ch2);
                }
                Compute(ch1);
                break;
            }
                    if (ch1 == 'z') b2 = false;
        }
            
    
    }
}
int main(){
    Calculator Calcul;
    cout << "请输入四则运算式:" << endl;
    Calcul.Cal();
    getchar();
    return 0;
}


编译错误图片:
![图片描述][1]


  [1]: /img/bVbaCGz
阅读 2.2k
1 个回答

it's my typo: data->date
Thanks for Chen's answer

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题