refs

M

Michael Sgier

Hello

In main.cpp:
CWorld* gameWorld = new CWorld; // do i need this?
CSimpEngine* MyCSimpEngine = new CSimpEngine;
CWorld *world = MyCSimpEngine->OnGetWorld();

world->Prepare(); //here i get the error below


in simpengine.h:
class CSimpEngine //: public CEngine
{
private:
CWorld *gameWorld;

public:
CWorld *OnGetWorld() { return gameWorld; }
}


in world.cpp:
CWorld::CWorld()
{
CTerrain* terrain = new CTerrain(256, 0.5);
}

world.cpp:59: error: ` terrain' undeclared (first use this function)
Why do i get the error? After my understanding OnGetWorld initializes
CWorld which constructor initializes terrain? Am i wrong? Do i need the
first line of main.cpp?
THANKS and regards from Switzerland
Michael
 
V

Victor Bazarov

Michael said:
In main.cpp:
CWorld* gameWorld = new CWorld; // do i need this?

The question you put in the comment: are you asking us or yourself?
If you're trying to ask us, then we don't know. You posted so damn
little of your code, there is no way to tell pretty much anything
from it.
CSimpEngine* MyCSimpEngine = new CSimpEngine;
CWorld *world = MyCSimpEngine->OnGetWorld();

world->Prepare(); //here i get the error below

Why don't you say what error you get, right here, why do you want us
to go look for it "below"? Is that the line 59 of 'world.cpp'? I do
not think so. So, what other error you're getting "below" that you
might be talking about?
in simpengine.h:
class CSimpEngine //: public CEngine
{
private:
CWorld *gameWorld;

public:
CWorld *OnGetWorld() { return gameWorld; }
}

Is this supposed to be relevant to the rest of it?
in world.cpp:
CWorld::CWorld()
{
CTerrain* terrain = new CTerrain(256, 0.5);
}

world.cpp:59: error: ` terrain' undeclared (first use this function)
Why do i get the error?

Are you sure you're getting *that* error? I can only think of 'CTerrain'
as being undeclared, not 'terrain'. 'terrain' declaration is actually
that particular statement, so the compiler can't complain that you have
not declared 'terrain' yet if you're just declaring it...
After my understanding OnGetWorld initializes
CWorld which constructor initializes terrain?

'terrain' is a local variable that exists during 'CWorld' default c-tor
execution.
Am i wrong?

Likely. Maybe. Wrong about what?
Do i need
the first line of main.cpp?

I don't know. What are you trying to accomplish with it?

V
 
M

Mike Wahler

Michael Sgier said:
Hello

In main.cpp:
CWorld* gameWorld = new CWorld; // do i need this?

Probably not. Probably sufficient is:

CWorld gameWorld;

... unless there's some specific reason which
you didn't state why need to dynamically allocate
your object. If you decide to stay with 'new',
don't forget to 'delete' the object when done
with it.
CSimpEngine* MyCSimpEngine = new CSimpEngine;

Same here. Is there some reason you can't simply
use:

CSimpEngine MyCSimpEngine;

(Doing it this way, the object will be automatically
destroyed when execution leaves the scope where it's
defined)
CWorld *world = MyCSimpEngine->OnGetWorld();
world->Prepare(); //here i get the error below


in simpengine.h:
class CSimpEngine //: public CEngine
{
private:
CWorld *gameWorld;

public:
CWorld *OnGetWorld() { return gameWorld; }
}


in world.cpp:
CWorld::CWorld()
{
CTerrain* terrain = new CTerrain(256, 0.5);
}

world.cpp:59: error: ` terrain' undeclared (first use this function)
Why do i get the error? After my understanding OnGetWorld initializes
CWorld

Not in the code you posted. All your 'OnGetWOrld()' function
does is return a pointer to a 'CWorld' object. No initialization
of anything is performed.
which constructor initializes terrain?

your 'OnGetWorld()' does not create any type 'CWorld' objects
(or any other type of object). So no 'CWorld()' constructor
is called.
Am i wrong?
Yes.

Do i need the
first line of main.cpp?

Depends upon what you want your program to do.

-Mike
 
J

John Harrison

Michael said:
Hello

In main.cpp:
CWorld* gameWorld = new CWorld; // do i need this?
CSimpEngine* MyCSimpEngine = new CSimpEngine;
CWorld *world = MyCSimpEngine->OnGetWorld();

world->Prepare(); //here i get the error below


in simpengine.h:
class CSimpEngine //: public CEngine
{
private:
CWorld *gameWorld;

public:
CWorld *OnGetWorld() { return gameWorld; }
}


in world.cpp:
CWorld::CWorld()
{
CTerrain* terrain = new CTerrain(256, 0.5);
}

world.cpp:59: error: ` terrain' undeclared (first use this function)
Why do i get the error? After my understanding OnGetWorld initializes
CWorld which constructor initializes terrain? Am i wrong? Do i need the
first line of main.cpp?
THANKS and regards from Switzerland
Michael

I'm afraid you are doing the same thing again. When you have compiler
error please post *ALL* the code. It is quite impossible to answer
either of the questions you asked on the code you have posted.

Your understanding about OnGetWorld is incorrect, but irrelevent anyway
because this is a compiler error.

Taking a complete guess I would say the your problem is because of not
including the correct header files, but of course I cannot tell BECAUSE
YOU DIDN'T POST ALL THE CODE.

Please, you've posted several times to this group, you ask good
questions but if you want you questions answered post all the code.

john
 
M

Michael Sgier

Hello again
as i'm new to C++ obvious things might look strange to me. Well i've
seen now the pointer and changed it to the code below. Why do i still
get the same error?
THANKS and regards
Michael

in world.cpp below I get the error: world.cpp:47: error: ` terrain'
undeclared (first use this function)

void CWorld::Animate(float deltaTime)
{
// set camera height based on position on terrain
camera->position.y = terrain->GetHeight(camera->position.x,
camera->position.z) + 10.0f;

terrain->Animate(deltaTime);
}

but i've in main.cpp:
CTerrain* terrain = new CTerrain(256, 0.5);

the constructor in the CTerrainclass is public
 
V

Victor Bazarov

Michael said:
as i'm new to C++ obvious things might look strange to me. Well
i've seen now the pointer and changed it to the code below. Why do i
still get the same error?
THANKS and regards
Michael

in world.cpp below I get the error: world.cpp:47: error: ` terrain'
undeclared (first use this function)

void CWorld::Animate(float deltaTime)
{
// set camera height based on position on terrain
camera->position.y = terrain->GetHeight(camera->position.x,

Did you intend to make 'terrain' a _member_ variable? If so, you need
to declare it in the _class_definition_, not in the constructor. Read
your favourite C++ book about _data_members_.
camera->position.z) + 10.0f;

terrain->Animate(deltaTime);
}

but i've in main.cpp:
CTerrain* terrain = new CTerrain(256, 0.5);

How is this statement related to the class 'CWorld'? Read the FAQ as
well while you're doing your reading. It will help you to get the most
of this newsgroup.
the constructor in the CTerrainclass is public

Yes, well, that's not how the language works. Don't guess what you need
to do, learn. And don't ask us to teach you, get a good book and study
C++ with it.

V
 
J

Jim Langston

Michael Sgier said:
Hello again
as i'm new to C++ obvious things might look strange to me. Well i've
seen now the pointer and changed it to the code below. Why do i still get
the same error?
THANKS and regards
Michael

in world.cpp below I get the error: world.cpp:47: error: ` terrain'
undeclared (first use this function)

void CWorld::Animate(float deltaTime)
{
// set camera height based on position on terrain
camera->position.y = terrain->GetHeight(camera->position.x,
camera->position.z) + 10.0f;

terrain->Animate(deltaTime);
}

but i've in main.cpp:
CTerrain* terrain = new CTerrain(256, 0.5);

the constructor in the CTerrainclass is public

As you say, you are instatizing terrain in main. But then you are trying to
use it in your class. Since terrain is not global (as it shouldn't be) your
class can not see your terrain object. Simplest thing to do: pass it into
the Animate function.

void CWorld::Animate(float deltaTime, CTerrain& terrain)

There are other ways to do this without passing it (such as passing a
pointer to it in the constructor and saving the pointer).
 
J

John Harrison

Michael said:
Hello again
as i'm new to C++ obvious things might look strange to me. Well i've
seen now the pointer and changed it to the code below. Why do i still
get the same error?
THANKS and regards
Michael

in world.cpp below I get the error: world.cpp:47: error: ` terrain'
undeclared (first use this function)

void CWorld::Animate(float deltaTime)
{
// set camera height based on position on terrain
camera->position.y = terrain->GetHeight(camera->position.x,
camera->position.z) + 10.0f;

terrain->Animate(deltaTime);
}

but i've in main.cpp:
CTerrain* terrain = new CTerrain(256, 0.5);

the constructor in the CTerrainclass is public

That's the problem isn't it. terrain is in main, but you are trying to
use it in CWorld::Animate.

You cound pass terrain as a parameter (presumably as a pointer or
reference) from main to the CWorld::Animate method.

Or you could make terrain a member variable of you CWorld class. I think
this is problem what you want to do, but you got the syntax wrong in the
last code you posted, and you've made matters worse in your latest code.

john
 
M

Michael Sgier

There are other ways to do this without passing it (such as passing a
pointer to it in the constructor and saving the pointer).
Hello Jim
What do you mean by saving the pointer? I tried it like that but still
got the same error.

CWorld::CWorld()
{
CTerrain* terrain = new CTerrain(256, 0.5);
}

Thanks and regards
Michael
 
J

Jim Langston

Michael Sgier said:
Hello Jim
What do you mean by saving the pointer? I tried it like that but still got
the same error.

CWorld::CWorld()
{
CTerrain* terrain = new CTerrain(256, 0.5);
}

Thanks and regards
Michael

Scope. That terrain pointer you declared and instatized in your constructor
only has a lifetime of the constructor itself. Once the constructor
finishes, the terrain object goes out of scope and gets deleted (although
not the memory you allocated with new which you no longer have a pointer to
and is a memory leak).

Declare terrain at your class scope.

class CWorld
{
....
private:
....
CTerrain* terrain;
....
};

Now in your constructor you can do:

CWorld::CWorld()
{
terrain = new CTerrain(256, 0.5);
}

But now you also have to have a destructor, since you allocated memory in
your constructor using new, every new must have an associated delete.

CWorld::~CWorld()
{
delete terrain;
}

Try this, fix your code. if you can't get it to work please paste your
entire CWorld class.
 
J

Jim Langston

In addition to my above answer, my original reply meant for you to do this:

class CWorld
{
private
....
CTerrain* terrain;
....
};

CWorld::CWorld(CTerrain* terr): terrain(terr) {}

If you are confused by that syntax, you could do the same thing with:

CWorld::CWorld(CTerrain* terr)
{
terrain = terr;
}

Notice, however, this method (in contrast to your method of creating the
terrain in your constructor).. this method doesn't create nor delete the
terrain object in your class.

IF you ONLY need the terrain object in your class, then it makes sense to
construct it in your class and delete it in your class.

If, however, you will be using terrain heavily outside your class, then
create it outside your class and pass the pointer (remembering to delete it
after everything is done using it).

If you will be using the terrain object lightly outside your class, then it
makes sense that it should still "belong" to your class, so you should still
create it in your class and delete it in your class, just create a public
method to return the pointer in your class.

CTerrain* CWorld::GetTerrain()
{
return terrain;
};

There are a few ways you could get the terrain into your class as well as
construct it. It depends on what you will be using it for mainly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top