简体   繁体   中英

error: ‘’ was not declared in this scope [C++]

cpp and .h` files for stack and when I tried to compile together, I get an error such like

error: 'top_index' was not declared in...

in two functions bool empty() and int size() .

My .cpp code is:

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

My .h file code is:

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;

};

Please someone help me what I did wrong? I even put include on cpp and causes problem :(

bool empty(){
return top_index == -1;
}

int size(){
return top_index + 1;
}

You forgot to declare these functions are class methods, like all other class methods you declared.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM