Instancing an object inside a class

Hi! I'm trying to make an Game Engine with C++ (and learn how this language works) and I'm having a problem in use in my program body an object instanced inside a class. Can somebody help me? Thank you!

EDIT: I'm trying to do this to just need call the Start of the Aura class to use the engine, and not of all classes.

Here is my code:

aura.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef AURA
#define AURA

class Aura
{
    public:
		Aura();
		~Aura();

        void Start();
        void Finish();		

		static bool *objAuraGrafico;
};

#endif 


aura.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "aura_grafico.h"
#include "aura.h"

Aura::Aura()
{
    Start();
}

Aura::~Aura()
{
    Finish();
}

void Aura::Start()
{
	AuraGrafico objAuraGrafico;
}

void Aura::Finish()
{
}



main.cpp
1
2
3
4
5
6
int main()
{
	Aura objAura;

	objAura.objAuraGrafico.Test(); // trying to use the object inside the Aura class
}
Last edited on
Should objAuraGrafico be a bool pointer?
Not really, but that's the only way that Visual Studio don't report an error...

what am I doing wrong?
If you want objAuraGrafico to be a AuraGrafico* you should change line 16 to static AuraGrafico* objAuraGrafico;, assuming you still want it to be static. Before the Aura class definition you put a forward declaration class AuraGrafico; to tell that AuraGrafico is a class type (this is needed to declare the pointer). static member variables are only declared inside the class definition so you need to define it somewhere else. In aura.cpp you put AuraGrafico*Aura::objAuraGrafico;


Line 16 in aura.cpp you are creating a local AuraGrafico object that will not exist outside that function. I don't think that is what you want. You probably want to dynamically allocate an AuraGrafico object objAuraGrafico = new AuraGrafico;.

The way you use it in main makes me unsure if you really want objAuraGrafico to be static. Why do you make it static? Maybe it doesn't need to be pointer either.
Last edited on
What I wanted is to have access to objAuraGrafico just by the object objAura. I changed my code according what you said and what I saw in an example a it's working now!

But I want to know if there is some concept error (I think its not), like the include that I done inside the file aura.h. I read once in the internet that you can't include files in .H, just in .CPP, is that right? =O

Thanks for your help!

aura.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef AURA
#define AURA

#include "aura_grafico.h"

class Aura
{
    public:
		Aura();
		~Aura();

        void Start();
        void Finish();		

	AuraGrafico objAuraGrafico;
};

#endif 


aura.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "aura.h"

Aura::Aura()
{
    Start();
}

Aura::~Aura()
{
    Finish();
}

void Aura::Start()
{
	AuraGrafico *objAuraGrafico = new AuraGrafico();

}

void Aura::Finish()
{
}


main.cpp
1
2
3
4
5
6
7
8
#include "aura.h"

int main()
{
	Aura objAura;

	objAura.objAuraGrafico.Test();
}
I read once in the internet that you can't include files in .H, just in .CPP, is that right?
No that's not right but it's good to try to avoid includes in headers because you easily get circular dependencies. Sometimes you have to include in the header. Forward declaration is only enough if you all you have is pointers to the type so in aura.h you have to aura_grafico.h because objAuraGrafico is no pointer.
Oh! I get it! Thanks for your help Peter! Things are getting cleaner now =)
Topic archived. No new replies allowed.