A class that uses another class

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 Color class and Painter class is Color. In Qt, I am getting the error that Painter is not a type. How can I fix this? What is the solution for this problem?

+3


source to share


1 answer


As Painter.h

you need to turn Color.h

, because you have objects of type Color. But in Color.h

you can add a forward declaration for the class Painter

:

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 define it in color.cpp

and there you includePainter.h

+3


source







All Articles