Also stoi and exit(0) are both out of scope in stk.cpp and I don't know why.
Here is main.cpp
#include "stk.h"
int main()
{
cout << "REDACTED
" << endl;
stk m;
m.startProg();
}
Upon compiling this with g++ -v main.cpp -o test
as results in this error:
undefined reference to 'stk::startProg()'
collect2: error: ld returned 1 exit status
And here is stk.h
#ifndef STK_H
#define STK_H
#include <iostream>
#include <string>
#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
And here is the startProg function in stk.cpp
int stk::startProg()
{
while (true)
{
string line = "";
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
", 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);
}
Formatting failed me, assume all brackets are correct.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…