我有一个问题,要么是我完全无法理解,要么很奇怪。这可能是第一个,但我整个下午都在谷歌上搜索没有成功,所以这里......
我有一个名为 Schedule 的类,它有一个 Room 向量作为成员。但是,当我使用 cmake 甚至手动编译时,我得到以下信息:
In file included from schedule.cpp:1:
schedule.h:13: error: ‘Room’ was not declared in this scope
schedule.h:13: error: template argument 1 is invalid
schedule.h:13: error: template argument 2 is invalid
schedule.cpp: In constructor ‘Schedule::Schedule(int, int, int)’:
schedule.cpp:12: error: ‘Room’ was not declared in this scope
schedule.cpp:12: error: expected ‘;’ before ‘r’
schedule.cpp:13: error: request for member ‘push_back’ in ‘((Schedule*)this)->Schedule::_sched’, which is of non-class type ‘int’
schedule.cpp:13: error: ‘r’ was not declared in this scope
以下是相关的代码:
#include <vector>
#include "room.h"
class Schedule
{
private:
std::vector<Room> _sched; //line 13
int _ndays;
int _nrooms;
int _ntslots;
public:
Schedule();
~Schedule();
Schedule(int nrooms, int ndays, int ntslots);
};
Schedule::Schedule(int nrooms, int ndays, int ntslots):_ndays(ndays), _nrooms(nrooms),_ntslots(ntslots)
{
for (int i=0; i<nrooms;i++)
{
Room r(ndays,ntslots);
_sched.push_back(r);
}
}
理论上,g++ 应该在包含它的类之前编译一个类。这里没有循环依赖,都是直截了当的东西。我完全被这个难住了,这让我相信我一定错过了一些东西。:-D
编辑:来自以下评论
的内容:room.h
#include <vector>
#include "day.h"
class Room
{
private:
std::vector<Day> _days;
public:
Room();
Room(int ndays, int length);
~Room();
};