I am experiencing problems with this function:

void move (Room&);

For some reason the compiler is saying that this is a variable or field, here is the implementation:

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

I have included Room into Monster.h

#include "Room.h"

I have been trying to figure this out searching everywhere, but I cannot find an example similar to mine.

Thanks for reading!

EDIT: This is what the compiler says: variable or field `move' declared void

Recommended Answers

All 2 Replies

Check the class definitions for Room and Monster, and make sure that there is a semi-colon after the last closing brace.

There is:

#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
#ifndef MONSTER_H
#define MONSTER_H

#include <vector>
#include "Explorer.h"
#include "Room.h"

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

Thanks for replying

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.