<classname> does not name a type

304
02 августа 2017, 21:37

Здравствуйте!

Есть код:

room.h

#ifndef ROOM_H
#define ROOM_H
#include <vector>
#include <cstdlib>
#include "point.h"
using std::vector;
class Room
{
private:
    int x;
    int y;
    int width;
    int height;
    vector<Point> points();
public:
    bool intersects(Room other);
    int area();
    Room(int x_, int y_, int w, int h);
    Room(){;}
    Room(const Room &base){(*this) = base;}

    static Room random(int fWidth, int fHeight, int maxWidth, int maxHeight, int minWidth, int minHeight);
    friend bool operator==(Room a, Room b);
    Room operator=(const Room &base);
};
#endif

point.h

#ifndef POINT_H
#define POINT_H
class Point
{
public:
    int x;
    int y;
    Point(int x_, int y_)
    {
        x = x_;
        y = y_;
    }
    Point()
    {
        ;
    }
};
#endif

maze.h

#ifndef MAZE_H
#define MAZE_H
#include <vector>
#include "room.h"
using std::vector;
enum Cell
{
    Empty,
    Room,
    Path
};
class Maze
{
    Cell *field;
    bool *fog;
    int width;
    int height;
    int area;
    void putRooms(int count);
    vector<Room> rooms;
public:
    Maze(int w, int h);
    void print(){}
};
#endif

При попытке скомпилировать, компилятор выдает:

In file included from main.cpp:2:0:
maze.h:27:13: error: type/value mismatch at argument 1 in template parameter lis
t for 'template<class _Tp, class _Alloc> class std::vector'
  vector<Room> rooms;
             ^
maze.h:27:13: note:   expected a type, got 'Room'
maze.h:27:13: error: template argument 2 is invalid
In file included from maze.cpp:1:0:
maze.h:27:13: error: type/value mismatch at argument 1 in template parameter lis
t for 'template<class _Tp, class _Alloc> class std::vector'
  vector<Room> rooms;
             ^
maze.h:27:13: note:   expected a type, got 'Room'
maze.h:27:13: error: template argument 2 is invalid

При этом, если сразу после vector<Rooms> rooms; в maze.h объявить

Point x;
vector<Point> y;

ошибок больше не станет.

Объясните, пожалуйста, в чем разница для компилятора между Point и Room, и как заставить код компилироваться?

Answer 1

У Вас член перечисления тоже назван Room, это перекрывает класс Room.

Переименуйте, используйте namespace или enum class.

READ ALSO
Как изменить значение WSA_MAXIMUM_WAIT_EVENTS?

Как изменить значение WSA_MAXIMUM_WAIT_EVENTS?

Например для FD_SET значение задается через FD_SETSIZEА как изменить значение константы WSA_MAXIMUM_WAIT_EVENTS?

288
Парсинг EBML Void элемент

Парсинг EBML Void элемент

Добрый день , хочу предупредить , что с такого рода заданиями сталкиваюсь в первые , потому могу спрашивать для кого-то очевидные вещи , прошу...

288
Как запретить обновления содержимого QListView в автоматическом режиме?

Как запретить обновления содержимого QListView в автоматическом режиме?

При соединении модели данных QAbstractTableModel к QListView данные обновляются автоматически (постоянно вызывается функция data)

341