How can my "Player" class access a function of my "Map" class?
The place I want it in is commented.
Thanks.

#include <iostream>
#include <string>

using namespace std;

class Map
{
      public:
             Map();
             ~Map();
             void Map::print();
             int Map::sendroom(int x_coord, int y_coord);
      private:
                int i;
                int p;
                int w;
                static const int MAP_HEIGHT = 9;
                static const int MAP_WIDTH = 9;
                int room_numbers[MAP_WIDTH][MAP_HEIGHT];
                static const int TOTAL_ROOMS = MAP_HEIGHT*MAP_WIDTH;
                string room_titles[0];
                string room_description[0];
                };
                
      Map::Map()
      {                
                room_numbers[MAP_HEIGHT][MAP_WIDTH];
                room_titles[TOTAL_ROOMS];
                room_description[TOTAL_ROOMS];
                w = 0;
                for (i=0; i < MAP_HEIGHT; i++)
                {
                    
                    for (p=0; p < MAP_WIDTH; p++)
                    {
                        room_numbers[i][p] = w;
                        w++;
                        }
                    } 
                }
                
      Map::~Map()
      {
                 }
                 
      void Map::print()
      {
           for (int a=0; a<MAP_HEIGHT; a++)
                {
                    for (int b=0; b<MAP_WIDTH; b++)
                    {
                        cout << room_numbers[a][b]<< " ";
                        }
                        cout << "\n";
                    }

                }
      int Map::sendroom(int x_coord, int y_coord)
      {
          cout << room_numbers[x_coord][y_coord];
          }

class Player
{
      public:
             Player();
             ~Player();
             void Player::gonorth();
      private:
                int room_numberx;
                int room_numbery;
                int new_room;
                };
      Player::Player()
      {
                      room_numberx = 0;
                      room_numbery = 0;
                      }
      Player::~Player()
      {
                       }
          
      void Player::gonorth()
      {
           room_numberx++;
           new_room = //Map.sendroom function goes here but I can't access it
           cout << "New room number: " << new_room << "\n";
           } 
                         
int main()
{
    Map map;
    Player player;
    player.gonorth();
    cin.get();
}

Recommended Answers

All 2 Replies

The only ways for Player to access functions in Map is one of the following

  1. Player is derived from Map, such as class Player : public Map
  2. The functions in Map are static and public.
  3. Player contains an instance of Map.

on line 11: you can remove the "Map::" because you are already in the class map and you are declaring a function.

just do this:

void print();

also at line 12 and line 68. ( remove the Map:: or Player:: )

How can my "Player" class access a function of my "Map" class?
The place I want it in is commented.

class Player
{
    public:
              Player();
             ~Player();
              void gonorth();
    private:
    int room_numberx;
    int room_numbery;
    int new_room;

    Map map; //this is the way you can use the functions in class Map into the class Player.
};
new_room = map.sendroom(room_numberx, room_numbery); //you can now use the function in the class Map to class Player
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.