为什么我会收到模板类堆栈的分段错误?

问题描述

不知道为什么会出现分段错误。我应该制作一个 rpn 计算器,它在将我的 Stack .h 和 .cpp 转换为模板类之前工作正常(在将其更改为模板类之前,它只能采用 int 数据类型)。请帮忙,对不起所有的代码! >

RPncalc.cpp 文件

#include <iostream>
#include "RPncalc.h"
#include "GenStack.h"

using namespace std;

RPncalc::RPncalc(){ //constructor

}

RPncalc::~RPncalc(){ //default

}

int RPncalc::calculator(string a,GenStack<int>* s1){
   int total;
   int num1;
   int num2;
   string filler = ""; //meant to convert char into a string
   int outcomeNum; //number that resulted from num1 and num2 operation
   for(int i = 0; i < a.size(); i++){
   if (a.at(i) != '+' && a.at(i) != '-' && a.at(i) != '*' && a.at(i) != '/' && a.at(i) != '%'){
       filler = filler + a.at(i);
       num1 = stoi(filler);
       s1 -> push(num1);
       filler = "";
   }
   else if(a.at(i) == '+'){
       num1 = s1 -> pop();
       num2 = s1 -> pop();
       outcomeNum = num2 + num1;
       s1 -> push(outcomeNum);
   }
   else if(a.at(i) == '-'){
       num1 = s1 -> pop();
       num2 = s1 -> pop();
       outcomeNum = num2 - num1;
       s1 -> push(outcomeNum);
   }
   else if(a.at(i) == '*'){
       num1 = s1 ->pop();
       num2 = s1 -> pop();
       outcomeNum = num2 * num1;
       s1 -> push(outcomeNum);
   }
   else if(a.at(i) == '/'){
       num1 = s1 -> pop();
       num2 = s1 -> pop();
       outcomeNum = num2 / num1;
       s1 -> push(outcomeNum);
   }
   else if(a.at(i) == '%'){
       num1 = s1 -> pop();
       num2 = s1 -> pop();
       outcomeNum = num2 % num1;
       s1 -> push(outcomeNum);
   }
   else{
       cout << "The value is not a letter nor operation" << endl;
   }
 }
 total = s1 -> peak();
 return total;
}

RPncalc.h

#include <iostream>
#include "GenStack.h"

using namespace std;

class RPncalc{
   public:
      RPncalc(); //default constructor
     ~RPncalc(); //default destructor
      int calculator(string a,GenStack<int>* s1); //takes a string and calculates based off rpn 
      calculating
};

这是我的 GenStack.h

#include <iostream>
#pragma once

using namespace std;

template <typename T>

struct node{
  public:
    T data;
    node<T>* next;
};

template <typename T>

class GenStack{
  private:
    node<T>* top;
    int maxnum;
  public:
    GenStack(){
      maxnum = 5;
      top = NULL;
    }

    ~GenStack(){
      node<T>* current = top;

      while (current){
        node<T>* next = current -> next;
        delete current;
        current = next;
      }
      top = NULL;
    }

    void push(const T& val){
      if(top == nullptr){
        top = new node<T>;
        top -> next = nullptr;
        top -> data = val;
      }
      else{
        node<T>* temp = new node<T>;
        temp -> data = val;
        temp -> next = top;
        top = temp;
      }
    }

    T pop(){
      if(top == nullptr){
        cout << "stack is empty" << endl;
        return 0;
      }
      else{
        node<T>* old = top;
        top = top -> next;
        return old -> data;
        delete(old);
      }
    }

    T peak(){
      if (top){
        return top -> data;
      }
      else
        cout << "Stack is empty" << endl;
    }

    void print(){
      node<T>* temp = top;
      while(temp != nullptr){
        cout << temp -> data << ",";
        temp = temp -> next;
      }
    }
};

这是我的 main.cpp

#include <iostream>
#include "RPncalc.h"
#include "GenStack.h"


using namespace std;

int main(int argc,char** argv)
{
    GenStack<int>* s1;
    RPncalc* calc = new RPncalc();
    string example = "23+";
    int total = calc -> calculator(example,s1);
    cout << total << endl;
    delete s1;
    delete calc;
}

解决方法

您之所以出错,是因为您忘记在 GenStack 中构造 main

    GenStack<int>* s1;

应该是

    GenStack<int> *s1 = new GenStack<int> ();

编译器应该告诉 ‘s1’ may be used uninitialized 如果您激活了适当的警告。