refs

Discussion in 'C++' started by Michael Sgier, Sep 12, 2005.

  1. 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 :-/
    Michael Sgier, Sep 12, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Sep 12, 2005
    #2
    1. Advertising

  3. Michael Sgier

    Mike Wahler Guest

    "Michael Sgier" <> wrote in message
    news:4325cae5$0$1161$...
    > 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
    Mike Wahler, Sep 12, 2005
    #3
  4. 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
    John Harrison, Sep 12, 2005
    #4
  5. 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
    Michael Sgier, Sep 13, 2005
    #5
  6. 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
    Victor Bazarov, Sep 13, 2005
    #6
  7. Michael Sgier

    Jim Langston Guest

    "Michael Sgier" <> wrote in message
    news:43271fcf$0$1163$...
    > 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).
    Jim Langston, Sep 13, 2005
    #7
  8. 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
    John Harrison, Sep 14, 2005
    #8

  9. > 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
    Michael Sgier, Sep 14, 2005
    #9
  10. Michael Sgier

    Jim Langston Guest

    "Michael Sgier" <> wrote in message
    news:432838cf$0$1154$...
    >
    >> 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.
    Jim Langston, Sep 14, 2005
    #10
  11. Michael Sgier

    Jim Langston Guest

    "Jim Langston" <> wrote in message
    news:01XVe.7662$...
    > "Michael Sgier" <> wrote in message
    > news:432838cf$0$1154$...
    >>
    >>> 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


    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.
    Jim Langston, Sep 14, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    sort and Sub Refs

    Guest, Jan 3, 2004, in forum: Perl
    Replies:
    1
    Views:
    717
    Guest
    Jan 3, 2004
  2. J Krugman
    Replies:
    0
    Views:
    355
    J Krugman
    Apr 15, 2004
  3. Jeremy Cowles

    Newb: Pointers, Refs, & Arrays

    Jeremy Cowles, Jul 21, 2003, in forum: C++
    Replies:
    5
    Views:
    349
    Jeremy Cowles
    Jul 22, 2003
  4. pmatos
    Replies:
    7
    Views:
    317
    Andre Kostur
    Jun 8, 2005
  5. John Nagle
    Replies:
    3
    Views:
    298
    John Nagle
    Feb 25, 2007
Loading...

Share This Page