Qt中的C++前瞻声明
C++ forward declaration in Qt
我有3个相互依赖的类:
class Channel
{
public:
enum ChannelType {
DIMMER, RED, GREEN, BLUE, STROBE
};
Channel(ChannelType type);
const ChannelType type;
void x(Fixture fixture);
};
class Fixture
{
public:
Fixture(int channel, FixturePattern pattern);
Channel getChannel(const Channel::ChannelType);
private:
const int channel;
FixturePattern pattern;
};
class FixturePattern
{
public:
FixturePattern(QList<Channel> channels);
Channel getChannel(Channel::ChannelType type);
private:
QList<Channel> channels;
};
这些类位于单独的头文件中。我试图将它们与#include
连接起来,但我总是以不完整的类型或XY was not declared in this scope
错误结束。有人可以解释我做错了什么吗?
我没有添加#include
,因为我昨天完全搞砸了。最近我已经发现了一个关于这个主题的问题,但我不想把它放在同一个文件中。可能吗?
在不涉及很多细节的情况下,您应该对类使用前向声明。您需要修改代码才能执行此操作。代码应如下所示。我没有测试它,但它应该可以工作。
class Fixture; // the forward deceleration
class Channel
{
public:
enum ChannelType {
DIMMER, RED, GREEN, BLUE, STROBE
};
Channel(ChannelType type);
const ChannelType type;
void x(const Fixture &fixture); // or void x(Fixture *fixture);
};
#include "FixturePattern.h"
class Fixture
{
public:
Fixture(int channel,const FixturePattern &pattern);
Channel getChannel(const Channel::ChannelType);
private:
const int channel;
FixturePattern pattern;
};
#include "Channel.h"
class FixturePattern
{
public:
FixturePattern(QList<Channel> channels);
Channel getChannel(Channel::ChannelType type);
private:
QList<Channel> channels;
};
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- c++类声明时,相同的例程,不同的成员变量类型
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别