这是我的代码:
这是itop.h:
using namespace std;#include <cstdlib>#include <iostream>class sNode{ public: char data; sNode *next;};class stack{ public: sNode *head; voID push (char); sNode pop(); int rank(char); stack() { cout << "Initiliazing stack." << endl; }};
这是我的itop.cpp文件:
#include "itop.h" voID stack::push (char a) { // cout << "Pushing " << a << endl; sNode *sn; sn = new sNode; sn->data = a; sn->next = head; head = sn; } sNode stack::pop() { // cout << "PopPing stack." << endl; sNode *sn; sn = head; head = head->next; return *sn; } int stack::rank(char x) { int num = 0; // cout << "Checking rank." << endl; if(x == 'using namespace std;#include <iostream>#include <cstdlib>#include <cstring>#include "itop.h"int main(){char *temp1; //Instantiating variables.char *temp2;temp1 = new char[20];temp2 = new char [20];stack s;do //Checking commands.{ cout << "infix_to_postfix> "; cin >> temp1; if(strcmp(temp1,"quit") == 0) { return 0; } if(strcmp(temp1,"convert") != 0) { cout << "Error! InvalID command." << endl; } cin >> temp2; if(strcmp(temp1,"convert") == 0) { for(int i=0; i<sizeof(temp2); i++) { if(isdigit(temp2[i])) { cout << atoi(&temp2[i]); } else if(s.rank(temp2[i]) < s.rank(s.head->data)) { sNode temp = s.pop(); cout << temp.data; } else { s.push(temp2[i]); } } } else { cout << "Error! Command not supported." << endl; }}while(strcmp(temp1,"quit") != 0);return 0;}') { num = 1; // cout << "Checking for null" << endl; return num; } else if(x == '+' || x == '-') { num = 2; // cout << "Checking if + or -" << endl; return num; // cout << "After return." << endl; } else if(x == '*' || x == '/') { num = 3; // cout << "Checking for * or /" << endl; return num; } else cout << "Error! input not valID!" << endl; }
这是main.cpp:
else if(s.rank(temp2[i]) < s.rank(s.head->data))
该函数被调用
else if(x == '+' || x == '-') { num = 2; // cout << "Checking if + or -" << endl; return num; // cout << "After return." << endl; }
问题出在这里:
stack() { head=NulL; cout << "Initiliazing stack." << endl; }
特别是在返回num之前,我得到“Segmentation fault(core dumped)”错误消息.我使用了gdb,所有我知道的就是在“检查是否 – 或”之后我看到“$1 = 2”.我不太清楚这意味着什么,但这是我想要回归的.
谢谢您的帮助.
解决方法 修复1:编写适当的构造函数.int stack::empty(){ if(head == NulL) return true; else return false;}
修复2:编写一个额外的方法来检查堆栈是否为空.
else if(!s.empty() && s.rank(temp2[i]) < s.rank(s.head->data)){ ...}
修复3:在使用堆栈数据之前检查堆栈是否为空.
修复4:修复其余的代码逻辑.
总结以上是内存溢出为你收集整理的c – 函数中return语句处的Seg Fault全部内容,希望文章能够帮你解决c – 函数中return语句处的Seg Fault所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)