另外stoi和exit(0)都在stk.cpp中超出范围,我不知道为什么。
这是main.cpp
#include "stk.h"
int main()
{
cout << "REDACTED\n" << endl;
stk m;
m.startProg();
}
使用g++ -v main.cpp -o test as 编译时会导致此错误:
undefined reference to 'stk::startProg()'
collect2: error: ld returned 1 exit status
这是stk.h
#ifndef STK_H
#define STK_H
#include
#include
#include "stdio.h"
using namespace std;
class stk
{
struct node
{
int data;
node * next;
};
node *head;
public:
stk()
{
head = NULL;
}
int push(int val);
int pop();
int display();
int reverse(node * temp);
void insertAtBottom(int tVal, node * temp);
bool isEmpty();
int startProg();
private:
};
#endif
这是stk.cpp中的startProg函数
int stk::startProg()
{
while (true)
{
string line = "\0";
getline(cin, line);
if (0 == line.compare(0,4, "push"))
{
int val = 0;
val = stoi(line.substr(5));
push(val);
}
else if(0 == line.compare (0,3, "pop"))
{
pop();
}
else if (0 == line.compare(0,7, "isempty"))
{
printf ("%s\n", isEmpty() ? "true" : "false");
}
else if (0 == line.compare(0,7, "reverse"))
{
node * top = NULL;
reverse(top);
}
else if (0 == line.compare(0,7, "display"))
{
display();
}
else if (0 == line.compare(0,4, "quit"))
{
exit(0);
}
格式化失败,我假设所有括号都正确。