C++ 错误:‘’;未在此范围[C+;+;]中声明
cppC++ 错误:‘’;未在此范围[C+;+;]中声明,c++,C++,cpp和.h`用于堆栈的文件,当我试图一起编译时,我得到了如下错误 error: 'top_index' was not declared in... 在两个函数中bool empty()和int size() 我的.cpp代码是: #include "char_stack.h" char_stack::char_stack(){ top_index = -1; } void char_stack::push(char item){ top_index = top_index + 1; A
和
.h`用于堆栈的文件,当我试图一起编译时,我得到了如下错误
error: 'top_index' was not declared in...
在两个函数中bool empty()
和int size()
我的.cpp
代码是:
#include "char_stack.h"
char_stack::char_stack(){
top_index = -1;
}
void char_stack::push(char item){
top_index = top_index + 1;
A[top_index] = item;
}
char char_stack::pop(){
top_index = top_index - 1;
return A[top_index + 1];
}
char char_stack::top(){
return A[top_index];
}
bool empty(){
return top_index == -1;
}
int size(){
return top_index + 1;
}
我的.h
文件代码是:
class char_stack
{
public:
char_stack();
void push( char item );
char pop();
char top();
bool empty();
int size();
private:
static const int capacity = 250000;
int A[capacity];
int top_index;
};
请有人帮帮我我做错了什么?我甚至在cpp上添加了include并导致问题:(
您忘记声明这些函数是类方法,就像您声明的所有其他类方法一样
bool empty(){
return top_index == -1;
}
int size(){
return top_index + 1;
}