PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 7, 2022

[FIXED] how to use objects of nested classes

 July 07, 2022     c++, class, nested     No comments   

Issue

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?


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



Answered By - Anoop Rana
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing