0

I have two classes each in Header files called "Map" and "Character". "Map" has a class called "map" and "Character" has a class called "character". Class "map" inherits class "character", and both have the header file of the other included. In one of the methods of class "map", I use a property member from class "character", and that works fine. However, when I try to use a "map" property member in class "character" it does not work. This happens regardless of whether or not "map" inherits "character".

This is class map which works fine:

#pragma once
#include <iostream>
#include "Character.h"
#include "AI.h"

using namespace std;

class map: public character
{
public:
static const int mapRow = 30; //-- all maps are the same size
static const int mapColumn = 116;
char marr[mapRow][mapColumn];

map()
{
    for (int i = 0; i<mapRow; i++)
    {
        for (int j = 0; j<mapColumn; j++)
        {
                marr[i][j] = ' ';//set whole array to blank
        }
    }
}

void display(void);
void level1(void);
void level2(void);
void level3(void);
void treeBlueprint(int);
};
//displays the map

void map::display(void)
{
    //This displays the level
    for (int i = 0; i<mapRow; i++)
    {
        for (int j = 0; j<mapColumn; j++)
        {
        cout << marr[i][j];
        }
    cout << "\n";
     }
}

This is class character that gives me the following errors when compiled:

  • 'map':is not a class or namespace name
  • 'map':is not a class or namespace name
  • 'mapColumn': undeclared identifier
  • 'mapRow': undeclared identifier

    #pragma once
    #include "Map.h"
    #include <iostream>
    using namespace std;
    
    class character
      {
        int hpPool = 100;
        int currentHp =hpPool;
    
     public:
         char Tommy ='?';
    int position1;
    int position2;
    char movement;
    
     character()
    {
        position1 = 5;
        position2 = 5;
       movement = '0';
     }
    
    void moveUp(void);
    void moveDown(void);
    void moveLeft(void);
    void moveRight(void);
    void moveFunct(void);
     };
    
          void character::moveUp(void)
         {
           if (position1 != 1)
          {
            position1--;
           }
            else
           {
           //Hitting a wall
          }
       }
    
        void character::moveDown(void)
        {
          if (position1 != (map::mapRow-2))
        {
          position1++;
        }
       else
       {
        //Hitting a wall
       }
    }
    
       void character::moveLeft(void)
    {
       if (position2 != 1)
        {
        position2--;
        }
         else
         {
          //Hitting a wall
         }
    }
    
        void character::moveRight(void)
       {
       if (position2 != (map::mapColumn-2))
       {
        position2++;
        }
       else
       {
            //Hitting a wall
        }
      }
    
    void character::moveFunct(void)
     {
        switch (movement)
        {
        case 'w': moveUp();
            break;
      case 'a': moveLeft();
          break;
       case 's': moveDown();
          break;
       case 'd': moveRight();
        break;
        }
      }
    
Nuthead
  • 49
  • 6

2 Answers2

2

Map.h is including Character.h and vice-versa, but that doesn't work (it would create infinite inclusion recursion if it weren't for the #pragma once).

Since character can not depend on map (because map is a derived class from character), it should not include Map.h.

André Sassi
  • 1,076
  • 10
  • 15
1

My suggestions to improve your code base:

  1. Remove using namespace std;.
  2. map is already a class in the std namespace. It will be better to use a different name or use it under a namespace of your own.

namespace MyApp
{
   class character { ... };
}

namespace MyApp
{
   class map : public character { ... };
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270