class inside of another class?

Hello people of the forum.
i'm having a problem, i make a class inside another class, and now i can't create(PS: i make in a .h/.cpp)the objects for use the functions of classes.
Source?!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HEADER:
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

class Graphic {
public :
        class Draw {
        public :
            void Pixel(COORD Pos, COLORREF color);
            void Line(COORD Init, COORD End, COLORREF Color);
            void Rectangle(COORD Pos, COORD Radius, COLORREF Color, float angle);
            void Circle(COORD Pos, int radius, COLORREF Color, float angle);
        };
};

#endif // FUNCTIONS_H_INCLUDED 

1
2
3
4
5
6
7
8
9
10
in main.cpp :
#include <windows.h>
#include "functions.h"

using namespace std;

main(){
    Graphic::Draw::Line({0,0}, {100,100}, RGB(255,255,255));
    return 0;
}
main.cpp|7|error: cannot call member function 'void Graphic::Draw::Line(COORD, COORD, COLORREF)' without object|
You're not creating an object, you're trying to call a member function as if it was static.
Create\expose an object of type Draw in Graphic say
class Graphic
{
//existing code
//
//
//new code
private:
Draw m_Draw;
public:
Draw GetDraw(){return m_Draw;}
}


Now create an object of type Graphic as
Graphic MyGraphicObj;

Now you can use like
MyGraphicObj.GetDraw().Line();

Note:-Exposing a class or member variable as public breaks encapsulation.
You should think\code always by remembering the laws of OOPS.
Last edited on
you could try:

1
2
   Graphic::Draw draw;
   draw.Line( ... );
i tryed
1
2
 Graphic::Draw draw;
   draw.Line( ... );

and this solved the error.
so Thanks all people what wrote suggestions in this topic.
and a special Thanks for SIK.
Topic archived. No new replies allowed.