gpt4 book ai didi

c++ - 使用另一个类的类

转载 作者:太空狗 更新时间:2023-10-29 21:19:21 28 4
gpt4 key购买 nike

我的类(class)有问题。

我有两个单独的标题。 Color.h 和 Painter.h:

1).颜色.h

class Color{
int number;
public:
void initialize();
void change (Painter draw);
}

2).画家.h

class Painter{
Color a,b;
public:
void get();
void draw();
}

我的问题是,我需要在 Color 类中使用 Painter,而 Painter 类使用 Color。在 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
}

方法 void change (Painter draw); 您将在 color.cpp 中定义它,并在其中包含 painter.h

关于c++ - 使用另一个类的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27629388/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com