Qt中的C++前瞻声明



我有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;
};

相关内容

  • 没有找到相关文章

最新更新