Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class that use another class

Tags:

c++

class

qt

I have a problem with my classes.

I have two separate headers. Color.h and Painter.h:

1). Color.h

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

2). Painter.h

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

My problem is, that i need to use Painter in class Color, and class Painter use Color. In Qt, i get a error that Painter is not a type. How can i fix this? What is solution for that problem?

like image 385
CROmpir Avatar asked Nov 19 '25 04:11

CROmpir


1 Answers

In Painter.h you need to include Color.h, because you have objects of type Color. But in color.h you can add a forward declaration for the Painter class:

class Painter;
class Color{
   int number;
public:
   void initialize();
   void change (Painter draw); //a forward declaration is enough for this
}

And the method void change (Painter draw); you will define it in the color.cpp and there you include painter.h

like image 137
Zlatomir Avatar answered Nov 21 '25 17:11

Zlatomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!