C++类没有正确包含在内

发布于 2024-08-30 11:33:40 字数 1583 浏览 6 评论 0原文

我有一个问题,要么是我完全无法理解,要么是非常奇怪。这可能是第一个,但是我花了整个下午的时间进行谷歌搜索,但没有成功,所以这里...

我有一个名为 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(); 
};

I have a problem which is either something I have completely failed to understand, or very strange. It's probably the first one, but I have spent the whole afternoon googling with no success, so here goes...

I have a class called Schedule, which has as a member a vector of Room. However, when I compile using cmake, or even by hand, I get the following:

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

Here are the relevant bits of code:

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

In theory, g++ should compile a class before the one that includes it. There are no circular dependencies here, it's all straightforward stuff. I am completely stumped on this one, which is what leads me to believe that I must be missing something. :-D

Edit:
The contents of room.h from the comments below:

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

class Room 
{ 
private: 
   std::vector<Day> _days; 

public: 
   Room(); 
   Room(int ndays, int length); 
   ~Room(); 
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别再吹冷风 2024-09-06 11:33:40

即使您省略了一些重要的代码(即 day.h 的内容),我的心灵调试器感觉告诉我您的头文件中存在循环依赖:

// schedule.h
#include "room.h"

// room.h
#include "day.h"

// day.h
#include "schedule.h"

这很糟糕。为了打破循环依赖,你需要弄清楚哪个文件不需要知道其他文件的具体实现细节。这是使用前向引用完成的。例如,我可以看到您对 Room 类的定义实际上不需要知道该类定义的 sizeof(Day) 是什么,因此您可以重写它如下:

#include <vector>
// do NOT include day.h

class Day;  // forward declaration
class Room 
{ 
private: 
   std::vector<Day> _days; 

public: 
   Room(); 
   Room(int ndays, int length); 
   ~Room(); 
};

现在room.h不再依赖于day.h,打破了循环依赖。当然,实现文件room.cpp仍然必须包含day.h

Even though you've omitted some important code (namely, the contents of day.h), my psychic debugger sense tells me that you have a circular dependency in your header files:

// schedule.h
#include "room.h"

// room.h
#include "day.h"

// day.h
#include "schedule.h"

This is bad. In order to break the circular dependency, you need to figure out which file doesn't need to know the concrete implementation details of the others. This is done using forward references. For example, I can see that your definition of the Room class doesn't actually need to know what sizeof(Day) is for the class definition, so you can rewrite it as follows:

#include <vector>
// do NOT include day.h

class Day;  // forward declaration
class Room 
{ 
private: 
   std::vector<Day> _days; 

public: 
   Room(); 
   Room(int ndays, int length); 
   ~Room(); 
};

Now room.h doesn't depend on day.h, breaking the circular dependency. Of course, the implementation file room.cpp will still have to include day.h.

墨小沫ゞ 2024-09-06 11:33:40

可能没关系,我但我看到你的标题中没有包含守卫。应该没关系,但只是为了覆盖任何角度......

It may not matter, I but I see no include guards in your headers. Shouldn't matter, but just to cover any angle...

断桥再见 2024-09-06 11:33:40

我无法从您的 Schedule.h/.cpp 帖子中看出,但看起来您可能在 Schedule.cpp 中包含#include“room.h”,但您的 Schedule.h 正在使用 Room 类。如果是这种情况,#include "room.h" 应该位于 Schedule.h 中。

或者您可以在schedule.h 中使用前向声明。

I can't tell from your post of schedule.h/.cpp but it looks like you might have the #include "room.h" in schedule.cpp but your schedule.h is making use of class Room. #include "room.h" should be in schedule.h if this is the case.

Or you can use forward declaration in schedule.h.

人事已非 2024-09-06 11:33:40

理论上,g++ 应该在包含该类的类之前编译该类。

g++ 应该能够按照它认为合适的任何顺序编译您的源文件。它在源代码中包含标头的顺序由#include语句的顺序设置。

最可能的情况是类名称是 room,而不是 Room。接下来可能的是,该名称是除 Room 之外的其他名称。它不太可能位于根命名空间以外的命名空间中。

编辑:好的,如果不是这些,请确保包含的 room.h 是您的 room.h 而不是其他 room.h。没有什么比编辑错误的文件副本更浪费时间的了。

编辑2:我假设您的头文件具有通常的包含一次结构:

#ifndef schedule_h
#define schedule_h

// header file code goes here.

#endif

...并且您为了简洁而省略了它。

编辑 3:我刚刚将您提供的代码复制到一个新目录,并创建了一个虚拟 day.h 文件,其内容如下:

typedef int Day;

然后我使用了 g++ -c -o Schedule.o Schedule。 cpp 来构建它并且没有错误。因此,该错误是我们看不到的。

编辑4:好的,健全性检查时间到了。查看 room.h 的顶部并确保它显示

#ifndef room_h

而不是

#ifdef room_h

In theory, g++ should compile a class before the one that includes it.

g++ should be able to compile your source files in any order it sees fit. The order it includes headers into your source is set by the order of your #include statements.

The most likely case is that the class name is room, and not Room. Next likely is that the name is some other thing besides Room. Less likely is that it is in a namespace other than the root namespace.

EDIT: Okay, if it's none of those, make sure that the room.h that is being included is your room.h and not some other room.h. Nothing like editing the wrong copy of a file to waste your day.

EDIT 2: I'm assuming your header files have the usual include-once structure:

#ifndef schedule_h
#define schedule_h

// header file code goes here.

#endif

... and that you omitted it for brevity.

EDIT 3: I just copied the code you gave to a new directory, and created a dummy day.h file with the contents:

typedef int Day;

I then used g++ -c -o schedule.o schedule.cpp to build it and got no errors. Therefore the error is something we're not seeing.

Edit 4: Okay, sanity check time. Look at the top of room.h and make sure it says

#ifndef room_h

and not

#ifdef room_h
笔芯 2024-09-06 11:33:40

Room.cpp 是什么样的?

另外..我从来没有遇到过任何问题,但也许你忘记在头文件的底部添加一行?

What does Room.cpp look like?

Also.. I've never had any problems with this, but maybe you forgot to put an extra line at the bottom of your header file?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文