如何解决为什么我会收到模板类堆栈的分段错误?
不知道为什么会出现分段错误。我应该制作一个 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
如果您激活了适当的警告。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。