0

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:

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?

LastHerd
  • 65
  • 7

1 Answers1

1

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

Jason
  • 36,170
  • 5
  • 26
  • 60
  • I just did that but now I have the problem in my main file when compiling that`'class Dmx' has no member named 'slider'` – LastHerd Jun 28 '22 at 10:18
  • @LastHerd See the [working demo](https://onlinegdb.com/0XswSpiz2). You need to provide the declarations for the member functions of `touchSlider` in the header and then you can define them in a source file called `touchSilder.cpp` as shown in my answer above. – Jason Jun 28 '22 at 10:31
  • I can also provide the declarations in my main file, right? I have a display with touchscreen (whose code is in my main file), then it should work too. – LastHerd Jun 28 '22 at 10:57
  • @LastHerd Do you mean member function definitions? If yes, then yes you can provide the definition for the member function `printChannel` and constructors inside the `main.cpp` file. Just make sure that across the whole program you provide only one definition for them though. – Jason Jun 28 '22 at 11:00
  • yes i meant member functions. It works now. But the only problem now is to create an object array of `touchSlider` INSIDE the `Dmx `class, where would i do this? @Anoop Rana – LastHerd Jun 28 '22 at 12:20
  • @LastHerd Take a look at [Separating class code into a header and cpp file](https://stackoverflow.com/questions/9579930/separating-class-code-into-a-header-and-cpp-file). It should help. If you still have follow up question, feel free to ask a new separate question describing your problem. As there is word limit in the comment section. – Jason Jun 28 '22 at 12:32