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
s 因为我昨天完全搞砸了。最近我已经找到了一个关于这个主题的 question 但我不想把它放在同一个文件中。可能吗?
无需详细说明,您应该为 类 使用前向声明。您需要修改代码才能执行此操作。代码应该看起来像这样。我没有测试它,但它应该可以工作。
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;
};
我有 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
s 因为我昨天完全搞砸了。最近我已经找到了一个关于这个主题的 question 但我不想把它放在同一个文件中。可能吗?
无需详细说明,您应该为 类 使用前向声明。您需要修改代码才能执行此操作。代码应该看起来像这样。我没有测试它,但它应该可以工作。
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;
};