Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I create objects of a nested class inside the parent class but in another header file?

I have a class Dmx with a nested class touchSlider. So before I had these classes in my main.cpp file and just created an object array of touchSlider within the Dmx class and it worked properly.

How can I implement this here, with different header files? The compiler gives an error message: invalid use of incomplete type 'class Dmx::touchSlider'

The object array is: touchSlider slider[10] = {50,130,210,290,370,50,130,210,290,370};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dmx.h

// dmx.h

#ifndef dmx_h
#define dmx_h

class Dmx {
  public:
    byte number;                                                

    Dmx(byte numberA) {                                                 
      number = numberA;
    }

    void settingsDisplay();
    
    class touchSlider;             // declaration of nested class

    touchSlider slider[10] = {50,130,210,290,370,50,130,210,290,370};  
};

#endif

touchSlider.h

// touchSlider.h

#ifndef touchSlider_h
#define touchSlider_h

#include "dmx.h"

class Dmx::touchSlider{
  private:
  int pos;                                     

  public:
  touchSlider(int posA){                        
    pos = posA;                      
  }

  void printChannel();

};

#endif

main.cpp

// main.cpp

#include "dmx.h"                
#include "touchSlider.h"

Dmx dmx[10] = {Dmx(1), Dmx(2),Dmx(3), Dmx(4), Dmx(5), Dmx(6), Dmx(7), Dmx(8), Dmx(9), Dmx(10)}; 


void Dmx::settingsDisplay() {
    // do something
}

void Dmx::touchSlider::printChannel() {
    // do something
}

My previous code (that worked great) where both classes where in the same file looked like this:

class Dmx {
  public:
    byte number;                                                

    Dmx(byte numberA) {                                                 
      number = numberA;
    }

    void channelDisplay(){
    }                                            

    void settingsDisplay(){
    }
    
    class touchSlider{
    private:
      int pos;                                     

    public:
      touchSlider(int posA){                        
         pos = posA;                      
      }

      void setChannel(/* some arguments*/){
      }

      void printChannel();
      }
   };             

    touchSlider slider[10] = {50,130,210,290,370,50,130,210,290,370};        
};
Dmx dmx[10] = {Dmx(1), Dmx(2),Dmx(3), Dmx(4), Dmx(5), Dmx(6), Dmx(7), Dmx(8), Dmx(9), Dmx(10)}; 

>Solution :

In order to create an array of touchSlider the compiler needs a definition of the touchSlider class. So as written this will not work.

In the code given

touchSlider slider[10] = {50,130,210,290,370,50,130,210,290,370};

the complier needs to know how big a touchSlider object is so it can allocate enough memory for a Dmx object. It also needs to know that a touchSlider can be constructed from an int. Both these things require the full definition of touchSlider.

Now maybe you can use some variation to achieve whatever your goal is, but without more details it’s hard to suggest anything.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading