Naq Z
Naq Z

Reputation: 39

Having public member variables access private members of the same class in C++

I'm trying to have a class in which the function changes the value of a private members of that class but I keep getting the error of "the use of an undeclared identifier." I thought if the function is a class member then they can access the private member?

My code for reference

Building.h

#ifndef BUILDING_H
#define BUILDING_H
#include "Point2D.h"
#include "GameObject.h"

class Building : public GameObject
{

private:
    unsigned int pokemon_count;

Building();
Building(char,int, Point2D);

public:
    void AddOnePokemon();
    void RemoveOnePokemon();
    void ShowStatus();
    bool ShouldBeVisible();

};

#endif

Building.cpp

#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;

Building::Building()
{
    display_code = 'B';
    location;
    id_num = ' ';
    state = '0';
    pokemon_count = 0;
    cout << "Building default constructed";
}

Building::Building(char in_code,int in_id, Point2D in_loc)
{
    id_num = in_id;
    location = in_loc;
    display_code = in_code;
    state = '0';
    cout << "Building constructed";
}

void AddOnePokemon()
{
    pokemon_count = pokemon_count+1;
}

void ReturnOnePokemon()
{
    pokemon_count = pokemon_count-1;
}

void ShowStatus()
{
    cout << "\"(" << pokemon_count << "pokemon is/are in this building";
}

Upvotes: 0

Views: 448

Answers (1)

Kai
Kai

Reputation: 216

Your functions are outside the scope of your class.

Add the class name in front of the functions and this should work:

void Building::AddOnePokemon()

Upvotes: 3

Related Questions