Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:‘’;未在此范围[C+;+;]中声明_C++ - Fatal编程技术网

C++ 错误:‘’;未在此范围[C+;+;]中声明

C++ 错误:‘’;未在此范围[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

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[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;
}