【发布时间】:2010-04-26 16:21:06
【问题描述】:
我有一个问题,要么是我完全无法理解,要么很奇怪。这可能是第一个,但我整个下午都在谷歌上搜索没有成功,所以这里......
我有一个名为 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的内容来自以下cmets:
#include <vector>
#include "day.h"
class Room
{
private:
std::vector<Day> _days;
public:
Room();
Room(int ndays, int length);
~Room();
};
【问题讨论】:
-
room.h 是什么样的?房间等级是大写还是小写?
-
Room是否在命名空间中定义? -
看起来像这样:#include
#include "day.h" class Room { private: std::vector _days;公共:房间();房间(整数天,整数长度); 〜房间(); }; -
请编辑您的问题以包含“room.h”的内容; cmets 中丢失了太多格式。
-
你能展示一下你用过的包含防护吗?会导致此问题的一件事是,如果您不小心在“schedule.h”和“room.h”中为守卫使用了相同的宏名称。