I get this compiler error: `Monster' was not declared in this scope, in this code:

#ifndef ROOM_H
#define ROOM_H

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <iostream>
#include "Monster.h"

using namespace std;

const int ROWS = 20;
const int COLS = 20;

class Room
{
    private:
        vector<int> indexes;
        static bool doNotCreateFloor;
        char map[ROWS][COLS];
        vector<Monster*> monsters;
        Room* rooms[6];
        
    public:
        Room (char floor, char extension, Room* room, char index);
        int getRows () { return ROWS; }
        int getCols () { return COLS; }
        void setUpMap ();  
        void printMap ();     
};

#endif

Tell me if you need monster.

Thanks for reading!

We need Monster.h.

Thanks for replying, I think I have figured out the problem, I was producing a circular referencing, so I am using now class forwarding such as this:

#ifndef MONSTER_H
#define MONSTER_H

#include "Item.h"
#include <vector>

class Room; // forwarding
class Explorer;

using namespace std;

class Monster
{
    private:
        int health;
        int x;
        int y;
        int strength;
        Item* item;
        
    public:
        Monster (int, int, int);
        void move (Room&);
        int getDamage ();
        void setItem (Item item) { this->item = &item; }
        int getX () { return x; }
        int getY () { return y; }
        int getHP () { return health; }
        void setHP (int hp) { health = hp; }
};

#endif

Although now I am getting this error: invalid use of undefined type `struct Room'

I will post the code that is producing this error:

void Monster::move (Room& room)
{             
    vector<int> iCoordinates;
    vector<int> jCoordinates;
    
    for (int i = x - 1 ; i <= x + 1 && i < room.getRows() ; i++)
    {
        for (int j = y - 1 ; j <= y + 1 && j < room.getCols() ; j++)
        {
            if (i < 0)
                break;
            else if (j < 0)
                continue;
            else if (i == x && j == y)
                continue;
            else if (room.map[i][j] == 'H')
            {
                Explorer hero = room.getHero();
                hero.setHP(hero.getHP() - getDamage());
                return;
            }
            else if (room.map[i][j] == '-')
            {
                iCoordinates.push_back(i);
                jCoordinates.push_back(j);
            }
        }
    }
    
    if (iCoordinates.size() > 0)
    {
        srand (time(NULL));
        int numberChosen = rand() % iCoordinates.size();
        
        room.map[x][y] = '-';
        room.map[iCoordinates.at(numberChosen)][jCoordinates.at(numberChosen)] = 'M';
        
        x = iCoordinates.at(numberChosen);
        y = jCoordinates.at(numberChosen);
    }       
}

The first line that produces it is: for (int i = x - 1 ; i <= x + 1 && i < room.getRows() ; i++)

Edited 4 Years Ago by Kuroshi: n/a

First determine if these are compile errors or linker errors. They look like compile errors to me, but I'm not guaranteeing it.

It seems like Monster needs Room and Room needs Monster, so perhaps each needs to #include each other. Simply declaring "class Room" isn't going to handle it I don't think. If you #include "Monster.h" in your "Room" file(s), that may do the trick or you may get some double define errors if you don't set up your #define guards or don't link correctly.

The safest bet, in my experience, is to do it the old fashioned way. .h files for the declarations, .c files for the implementation. It looks like you're trying to do it all in a .h file, which is often easy enough, but can get tricky when you have scenarios like you have (each class needs each other).

ROWS and COLS would likely be in ROOM.c and then declared as extern in ROOM.h so you don't get "variable declared twice" type errors when you link.


Anyway, that's the method I'd use. If it's not working the way you have it, replace line 7 with #include "Room.h". My theory is that will probably get rid of the error you have, but then add some new ones. See above for the way I would approach it. Some people in this forum might be able to tell you EXACTLY what you need to do and perhaps they'll weigh in, but I'm not one of them. I'm an "experiment till it works" type of guy. :)

Keep in mind that it also isn't always the code itself. Sometimes it's the compile / linkage order in the Makefile.

This question has already been answered. Start a new discussion instead.