C++ 使用其他类的类
我的课有问题 我有两个单独的标题。颜色.h和油漆工.h: 1) 。颜色 2) 。画家C++ 使用其他类的类,c++,qt,class,C++,Qt,Class,我的课有问题 我有两个单独的标题。颜色.h和油漆工.h: 1) 。颜色 2) 。画家 class Painter{ Color a,b; public: void get(); void draw(); } 我的问题是,我需要在类中使用画师颜色,而类画师使用颜色。在Qt中,我得到一个错误,即Painter不是类型。我怎样才能解决这个问题?该问题的解决方案是什么?在Painter.h中,您需要包括Color.h,因为您有Color类型的对象。 但是在co
class Painter{
Color a,b;
public:
void get();
void draw();
}
我的问题是,我需要在类中使用画师颜色,而类画师使用颜色。在Qt中,我得到一个错误,即Painter不是类型。我怎样才能解决这个问题?该问题的解决方案是什么?在
Painter.h
中,您需要包括Color.h
,因为您有Color类型的对象。
但是在color.h
中,可以为Painter
类添加转发声明:
class Painter;
class Color{
int number;
public:
void initialize();
void change (Painter draw); //a forward declaration is enough for this
}
和方法<代码>空隙变化(画家绘制)您将在
color.cpp
中定义它,在这里您包括painter.h
您可以通过在另一个类中向前声明一个类来实现这一点,并且只使用指向向前声明的类的指针。但是如果你在这样一个简单的情况下有循环依赖性,考虑改变你的软件的设计,你不需要改变<代码>空洞变化(画家绘制);<代码>至<代码>无效更改(油漆工和绘图)这样行吗?@drescherjm我也在考虑,但看起来没问题()@Zlatomir:这是不正确的解释:,因为你没有在那里构造对象
。您不需要它,但不是因为它,因为您也不在头中构造基类,但是您需要它。
class Painter;
class Color{
int number;
public:
void initialize();
void change (Painter draw); //a forward declaration is enough for this
}