下面是abc.cpp文件
bool Stack::push(int n)
{
node *p=new node;
if(p==NULL)
{
cout<<"the stack is overflow !\n";
return false;
}
else
{
p->data=n;
p->next=top;
top=p;
return true;
}
}
bool Stack::pop(int& n)
{
if(top==NULL)
{
cout<<"the stack is empty !\n";
return false;
}
else
{
node *p=top;
n=top->data;
top=top->next;
delete p;
return true;
}
}
下面是abc.h文件
class Stack
{
private:
struct node
{
int data;
node *next;
}*top;
public:
Stack()
{
top=NULL;
}
bool push(int n);
bool pop(int& n);
};
下面是main.cpp文件
#include"stdio.h"
#include"iostream.h"
#define MAXSIZE 100
#include"abc.h"
#include"abc.cpp"
int main()
{
Stack s;
int i=0;
s.push(5);
s.push(7);
s.pop(i);
cout<<i<<endl;
s.pop(i);
cout<<i<<endl;
return 0;
}
出现好多错误,帮忙看一下
main.cpp
#include"stdio.h"
#include"iostream"
#include "abc2.h"
using namespace std;
#define MAXSIZE 100
int main()
{
Stack s;
int i=0;
s.push(5);
s.push(7);
s.pop(i);
cout<<i<<endl;
s.pop(i);
cout<<i<<endl;
return 0;
}
abc.h
#include <iostream>
using namespace std;
class Stack
{
private:
struct node
{
int data;
node *next;
}*top;
public:
Stack()
{
top=NULL;
}
bool push(int n);
bool pop(int& n);
};
abc2.h //之前cpp不行必须是头文件
#include "abc.h"
bool Stack::pop(int& n)
{
if(top==NULL)
{
cout<<"the stack is empty !\n";
return false;
}
else
{
node *p=top;
n=top->data;
top=top->next;
delete p;
return true;
}
}
bool Stack::push(int n)
{
node *p=new node;
if(p==NULL)
{
cout<<"the stack is overflow !\n";
return false;
}
else
{
p->data=n;
p->next=top;
top=p;
return true;
}
}
main.cpp
#include<iostream.h>
#define MAXSIZE 100
#include"abc.h"
void main()
{
Stack s;
int i=0;
s.push(5);
s.push(7);
s.pop(i);
cout<<i<<endl;
s.pop(i);
cout<<i<<endl;
}
////////////////abc.cpp
#include<iostream.h>
#include"abc.h"
bool Stack::push(int n)
{
node *p=new node;
if(p==NULL)
{
cout<<"the stack is overflow !\n";
return false;
}
else
{
p->data=n;
p->next=top;
top=p;
return true;
}
}
bool Stack::pop(int& n)
{
if(top==NULL)
{
cout<<"the stack is empty !\n";
return false;
}
else
{
node *p=top;
n=top->data;
top=top->next;
delete p;
return true;
}
}
///////////////////abc.h
#ifndef ABC_H
#define ABC_H
class Stack
{
private:
struct node
{
int data;
node *next;
}*top;
public:
Stack()
{
top=NULL;
}
bool push(int n);
bool pop(int& n);
};
#endif