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 to use objects of nested classes

I have a class in a header file:

dmx.h

// dmx.h
#ifndef dmx_h
#define dmx_h

class Dmx {
  public:
    Dmx() {   
    }                                    
    // some functions and variables
    void channelDisplay() {
    }

    class touchSlider;     
};
     
#endif

and a nested class in another header file:

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

touchSlider.h

// touchSlider.h
#ifndef touchSlider_h
#define touchSlider_h

#include "dmx.h"

class Dmx::touchSlider{
  private:
  // some variables

  public:
  touchSlider(){                       
  }
  // some functions
  void printChannel() {
  }
};
   

#endif

And I initialize my objects in my main file like this:

// 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)};

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

// functions:
Dmx::touchSlider::printChannel() {}
Dmx::channelDisplay() {}      

There is an error message when compiling saying: 'class Dmx' has no member named 'slider'

Could someone explain to me how this works correctly?

>Solution :

The problem is that inside the class dmx you already provided a definition for the nested class touchSlider since you used {} and so you’re trying to redefine it inside touchSlider.h.

To solve this you can provide the declarations for the member functions of touchSlider in the header and then define those member functions inside a source file named touchSlider.cpp as shown below:

dmx.h

// dmx.h
#ifndef dmx_h
#define dmx_h

class Dmx {
  public:
    Dmx() {   
    }  
    Dmx(int){
    }
    // some functions and variables
    void channelDisplay() {
    }

    class touchSlider{
        private:
        // some variables

        public:
            touchSlider(); //declaration 
           touchSlider(int);//declaration
            
            // some functions
            void printChannel();//declaration
    };
};

#endif

touchSlider.cpp

#include "dmx.h"
//default ctor implementation
Dmx::touchSlider::touchSlider(){
 
}
//definition
void Dmx::touchSlider::printChannel() {
  
}
//definition
Dmx::touchSlider::touchSlider(int)
{
    
}

Working demo

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