PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Need Help with POO

ok this time i try POO for my next project... i'm stuck with i think a very basic problem:

(using VC++)

this is myclassdef.h:
Code:

class CEngine
{
public:
int m_X;
int m_Y;
CEngine();
~CEngine();
void Init(int x, int y );
};


this is myclass.cpp
Code:

#include "myclassdef.h"

CEngine() // constructor
{
return NULL;
}

CEngine::~CEngine() // destructor
{
}

void CEngine::Init( int x, int y } // method Init
{
m_X = x; // <===== Stuck Here !
m_Y = y;
}


ok looks very simple staff

then myframegame.h where i want to declare an instance of my class:
Code:

#include "Framegame.h"
#include "Factory.h"
#include "myclassdef.h"

using namespace Overloaded;

class mygame : public FrameGame
{
public:
void InitGameState();
void DoGameState();
private:
ScopedPtr<CEngine> myobject;
};


then i try in the InitGameState()
Code:

myobject->Init( 120, 120 );


compil & link ok, .exe generated

here is the probleme:
i get an ** access violation ** in the CEngine::Init() body on the line where i set: m_X = x; as if i had no rights to access m_X or m_Y :? in the debugger, i can see on m_X and m_Y: ** expression can't be evaluated **

what did i do ?!? did i miss a "xxxx.h" to load for this to work ? please help me to have my first class working :x
Ah, now I see the error. :) In inigamestate you use the CEngine pointer without instantiating it first.

If you would have a CEngine member like this:

Code:

class A
{
CEngine myengine;
};

it would get instantiated when you instantiate class A. However, if you do it like this:

Code:

class A
{
CEngine* myengine;
};

then 'myengine' is undefined until you fill it with something, like so:

Code:

myengine = new CEngine();

Same goes for the scoped pointer thingy:

Code:

class A
{
ScopedPtr<CEngine> myengine;
};

Now you need to do something like this in your cpp file:

Code:

myengine.Reset( new CEngine() );

And after that you can call the init method.

- Jacco.
odd

i must have something else wrong, i first tried a simple:
Code:

class A
{
CEngine myengine;
};

which should request no additional instantiation

then i got an ** LNK2001: unresolved external symbol "public: __thiscall CEngine::CEngine(void)" **

that led me to use ScopedPtr where i can link but not access the class var as you already know

if i add the
Code:

myengine.Reset( new CEngine() );

that you suggest, i come back to the same unresolved external

can that come from namespace ?
:cry:
I'm not sure about that. Perhaps. Can someone else answer that? :)
i found something:

if i declare the constructor as:

Code:

class myengine
{
public:
myengine() { };
~myengine();
}


i have no unresolved external

but if i code it like this:

(myengine.h)
Code:

class myengine
{
public:
myengine();
~myengine();
}


(myengine.cpp)
Code:

myengine()
{
}

myengine::~myengine()
{
]


it compiles well !
Yes of course, that's the problem. :) You didn't define the constructor in the first example, and thus there's no way to construct CEngine. That causes the unresolved external.
okay my first POO class works now :')
THANKS !
(i thought the constructor was well describe in my first .h & .cpp at the top of this message, i'm not sure to understand why it didn't work / why it works now ... anyway, POO has its secrets ... :D )

<<you know what ? i'm happy ... bouh>>
Do you not want the constructor defined as:

myengine::myengine()

in the c file?
heu... i believe this leads to a compil error but... it's been many many tries since then, i'll try again :) thanks
hi again,

ok, my POO seems to work well know :)

i understood that i have to free my objects when i shutdown, to have no memory leaks ?

can someone give a small info on what i have to delete and how ? (for any instance of an overloaded object Surface, Sprite, aso... or for my own objects where i use variables, pointers, arrays of variables...)

ThX
The way the memory manager works in Overloaded is that they add anything allocated to a list(the memory location). So if you let's say create a surface using the factory, the factory will append the memory to the list.

Now if you want to delete the memory allocated by factory, you must also remove that memory from the list. Since we really can't use the release function in factory (I tried :) ) you must use the 'scope pointer' provided by the library.

So anything you create using the factory/tools/api must then be put in 'scope pointer'. Whenever the 'scope pointer' is reset or deleted, it will call the release function from the factory.

Code:

ScopedPtr< Surface > g_menuBkgrnd;
//...
g_menuBkgrnd.Reset(g_Factory->NewSurface("bkgrnd.dat"));
//...
/*since g_menuBkgrnd is global, I'll use reset to free the memory*/
g_menuBkgrnd.Reset();
D: You definitely do not need to use ScopedPtr's in order to have them removed from the list. If you use regular pointers, the memory is released from the list just as well. This all happens in the virtual destructor in the MemoryTracker class, which all tracked objects derive from.

As for the original question:

1. Why are you returning NULL from your constructor?
2. What is POO? Programming Object Oriented?

Cheers,

Jape

1. Why are you returning NULL from your constructor?
2. What is POO? Programming Object Oriented?



hi master :p

1. Because the "OOP" tutorial i found on the Net did :o) ....
2. POO is "Programmation orientée objet"... sorry about this, is it "OOP" (for Object Oriented Programming) in english ? :p