Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > refs

Reply
 
 
Michael Sgier
Guest
Posts: n/a
 
      09-12-2005
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 should have become a Tennis No.1 like my neighbor Federer...well now it's
too late :-/
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-12-2005
Michael Sgier wrote:
> 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


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      09-12-2005

"Michael Sgier" <(E-Mail Removed)> wrote in message
news:4325cae5$0$1161$(E-Mail Removed)...
> 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-12-2005
Michael Sgier wrote:
> 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
 
Reply With Quote
 
Michael Sgier
Guest
Posts: n/a
 
      09-13-2005
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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-13-2005
Michael Sgier wrote:
> 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


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-13-2005

"Michael Sgier" <(E-Mail Removed)> wrote in message
news:43271fcf$0$1163$(E-Mail Removed)...
> 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).


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      09-14-2005
Michael Sgier wrote:
> 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
 
Reply With Quote
 
Michael Sgier
Guest
Posts: n/a
 
      09-14-2005

> 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
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      09-14-2005
"Michael Sgier" <(E-Mail Removed)> wrote in message
news:432838cf$0$1154$(E-Mail Removed)...
>
>> 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


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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are weak refs slower than strong refs? John Nagle Python 3 02-25-2007 08:54 PM
Strings and refs, need comments on code pmatos C++ 7 06-08-2005 05:05 PM
ISO refs on XML technologies design J Krugman XML 0 04-15-2004 09:51 PM
sort and Sub Refs Perl 1 01-03-2004 09:43 PM
Newb: Pointers, Refs, & Arrays Jeremy Cowles C++ 5 07-22-2003 02:30 PM



Advertisments