需要一些关于 class 和头文件的建议
Need some advice with a class and a header file
我想做什么?
我有两个代码,一个 .cpp 和一个 .h,我想通过从 cpp 调用 POP 和 PUSH 两种方法来创建堆栈
输出错误(更新)
错误:PUSH(int val) 不能重载(与 POP() 相同)
用 g++ -Wall -O2 编译 Stack.cpp -o Stack
cpp代码
# include < iostream >
# include " LibStack.h "
using namespace std;
using namespace STACK;
int main()
{
Stack S1;
int elm;
cout << "Insert value:"<< endl;
cin >> elm;
S1.PUSH(elm);
S1.POP();
return 0;
}
头文件
# ifndef _LibStack_H_
# define _LibStack_H_
# define MAX_STACK 10
using namespace std;
namespace STACK
{
class Stack
{
private:
int stack[MAX_STACK];
int MIN_STACK = 0;
public:
void PUSH(int);
void POP();
PUSH(int val)
{
if(MIN_STACK < MAX_STACK)
{
stack[MAX_STACK+1] = val;
}
else
cout << "Full stack!" << endl;
}
POP()
{
int aux;
if(MIN_STACK >= 0)
{
aux = stack--[MIN_STACK];
cout << " POP " << endl << aux << endl;
}
else
cout << "Empty stack!" << endl;
}
};
}
# endif // __LibStack_H_
下面是一些随机点,不假装是一个完整的列表。
class Stack // template <class Stack>
// void PUSH(int);
// void POP();
void PUSH(int val) // PUSH(int val)
stack[MIN_STACK++] = val; // MIN_STACK += 1; stack[MAX_STACK] = val;
void POP() // POP()
// if(MIN_STACK >= 0) { MIN_STACK -= 1; aux = stack[MIN_STACK+1];
if(MIN_STACK > 0) { aux = stack[--MIN_STACK];
您可以这样定义 class:
class Stack
{
private:
int stack[MAX_STACK];
int MIN_STACK = 0;
public:
. . .
}
推送功能可以实现如下:
当你在插入前增加MIN_STACK的值时,你总是让堆栈[0]为空并浪费了space。也使用 MIN_STACK
作为索引而不是 MAX_STACK
因为 MAX_STACK
的值总是 10.
void PUSH(int val)
{
if(MIN_STACK < MAX_STACK)
{
stack[MIN_STACK++] = val;
/*
Here MIN_STACK is incremented after insertion. It is the same as stack[MIN_STACK] = val;
MIN_STACK +=1;
*/
}
else
cout << "Full stack!" << endl;
}
在 POP 函数中,如果 MIN_STACK
为 0,则无需删除任何内容,因为每次压入一个值时,MIN_STACK
都会递增。MIN_STACK
始终指向下一个空闲位置。所以要弹出的数据在第(MIN_STACK-1)
个位置。所以减少 MIN_PATH
并使用它。
void POP()
{
int aux;
if(MIN_STACK > 0)
{
aux = stack[--MIN_STACK];
/* Here MIN_STACK is decremented before popping out. It is the same as MIN_STACK -= 1;
aux = stack[MIN_STACK]; */
cout << " POP : " << aux << endl;
}
else
cout << "Empty stack!" << endl;
}
在您的 cpp
文件中创建一个 class 的对象:
Stack S1;
S1.PUSH(elm);
S1.POP();
我想做什么?
我有两个代码,一个 .cpp 和一个 .h,我想通过从 cpp 调用 POP 和 PUSH 两种方法来创建堆栈
输出错误(更新)
错误:PUSH(int val) 不能重载(与 POP() 相同)
用 g++ -Wall -O2 编译 Stack.cpp -o Stack
cpp代码
# include < iostream >
# include " LibStack.h "
using namespace std;
using namespace STACK;
int main()
{
Stack S1;
int elm;
cout << "Insert value:"<< endl;
cin >> elm;
S1.PUSH(elm);
S1.POP();
return 0;
}
头文件
# ifndef _LibStack_H_
# define _LibStack_H_
# define MAX_STACK 10
using namespace std;
namespace STACK
{
class Stack
{
private:
int stack[MAX_STACK];
int MIN_STACK = 0;
public:
void PUSH(int);
void POP();
PUSH(int val)
{
if(MIN_STACK < MAX_STACK)
{
stack[MAX_STACK+1] = val;
}
else
cout << "Full stack!" << endl;
}
POP()
{
int aux;
if(MIN_STACK >= 0)
{
aux = stack--[MIN_STACK];
cout << " POP " << endl << aux << endl;
}
else
cout << "Empty stack!" << endl;
}
};
}
# endif // __LibStack_H_
下面是一些随机点,不假装是一个完整的列表。
class Stack // template <class Stack>
// void PUSH(int);
// void POP();
void PUSH(int val) // PUSH(int val)
stack[MIN_STACK++] = val; // MIN_STACK += 1; stack[MAX_STACK] = val;
void POP() // POP()
// if(MIN_STACK >= 0) { MIN_STACK -= 1; aux = stack[MIN_STACK+1];
if(MIN_STACK > 0) { aux = stack[--MIN_STACK];
您可以这样定义 class:
class Stack
{
private:
int stack[MAX_STACK];
int MIN_STACK = 0;
public:
. . .
}
推送功能可以实现如下:
当你在插入前增加MIN_STACK的值时,你总是让堆栈[0]为空并浪费了space。也使用 MIN_STACK
作为索引而不是 MAX_STACK
因为 MAX_STACK
的值总是 10.
void PUSH(int val)
{
if(MIN_STACK < MAX_STACK)
{
stack[MIN_STACK++] = val;
/*
Here MIN_STACK is incremented after insertion. It is the same as stack[MIN_STACK] = val;
MIN_STACK +=1;
*/
}
else
cout << "Full stack!" << endl;
}
在 POP 函数中,如果 MIN_STACK
为 0,则无需删除任何内容,因为每次压入一个值时,MIN_STACK
都会递增。MIN_STACK
始终指向下一个空闲位置。所以要弹出的数据在第(MIN_STACK-1)
个位置。所以减少 MIN_PATH
并使用它。
void POP()
{
int aux;
if(MIN_STACK > 0)
{
aux = stack[--MIN_STACK];
/* Here MIN_STACK is decremented before popping out. It is the same as MIN_STACK -= 1;
aux = stack[MIN_STACK]; */
cout << " POP : " << aux << endl;
}
else
cout << "Empty stack!" << endl;
}
在您的 cpp
文件中创建一个 class 的对象:
Stack S1;
S1.PUSH(elm);
S1.POP();