Question about pointers and vectors

Discussion in 'C++' started by JoeC, Dec 6, 2005.

  1. JoeC

    JoeC Guest

    vector<Sprite*>box;
    ++++++++++++++++++++++++++++++++++
    tank = new Bitmap(hdc, IDB_IMAGE2, g_hin);
    Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE);
    break;

    box.push_back(ms);

    +++++++++++++++++++++++++++++++++++++++

    for(vector<Sprite*>::iterator itr = box.begin();
    itr != box.end(); itr++){
    itr->Update();
    }

    This isn't working how can I do it better?
    JoeC, Dec 6, 2005
    #1
    1. Advertising

  2. JoeC

    Andre Kostur Guest

    "JoeC" <> wrote in news:1133898039.843277.38830
    @o13g2000cwo.googlegroups.com:

    >
    >
    > vector<Sprite*>box;
    > ++++++++++++++++++++++++++++++++++
    > tank = new Bitmap(hdc, IDB_IMAGE2, g_hin);
    > Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE);
    > break;
    >
    > box.push_back(ms);
    >
    > +++++++++++++++++++++++++++++++++++++++
    >
    > for(vector<Sprite*>::iterator itr = box.begin();
    > itr != box.end(); itr++){
    > itr->Update();
    > }
    >
    > This isn't working how can I do it better?


    1st... handy to post what you mean by "This isn't working".

    2nd... the type of *itr is Sprite*

    So.... (*itr)->Update();
    Andre Kostur, Dec 6, 2005
    #2
    1. Advertising

  3. JoeC

    Mike Wahler Guest

    "JoeC" <> wrote in message
    news:...
    >
    >
    > vector<Sprite*>box;
    > ++++++++++++++++++++++++++++++++++
    > tank = new Bitmap(hdc, IDB_IMAGE2, g_hin);
    > Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE);
    > break;
    >
    > box.push_back(ms);
    >
    > +++++++++++++++++++++++++++++++++++++++
    >
    > for(vector<Sprite*>::iterator itr = box.begin();
    > itr != box.end(); itr++){
    > itr->Update();

    -
    > }
    >
    > This isn't working how can I do it better?


    (*itr)->Update();

    -Mike
    Mike Wahler, Dec 6, 2005
    #3
  4. On 6 Dec 2005 11:40:39 -0800, "JoeC" <> wrote:

    >
    >
    >vector<Sprite*>box;
    >++++++++++++++++++++++++++++++++++
    > tank = new Bitmap(hdc, IDB_IMAGE2, g_hin);
    > Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE);
    > break;
    >
    > box.push_back(ms);
    >
    >+++++++++++++++++++++++++++++++++++++++
    >
    > for(vector<Sprite*>::iterator itr = box.begin();
    > itr != box.end(); itr++){
    > itr->Update();
    >}
    >
    >This isn't working how can I do it better?


    WHAT isn't working?? No way to tell from this snippet of incomplete
    code.

    --
    Bob Hairgrove
    Bob Hairgrove, Dec 6, 2005
    #4
  5. JoeC wrote:
    > vector<Sprite*>box;
    > ++++++++++++++++++++++++++++++++++
    > tank = new Bitmap(hdc, IDB_IMAGE2, g_hin);
    > Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE);
    > break;
    >
    > box.push_back(ms);
    >
    > +++++++++++++++++++++++++++++++++++++++
    >
    > for(vector<Sprite*>::iterator itr = box.begin();
    > itr != box.end(); itr++){
    > itr->Update();
    > }
    >
    > This isn't working how can I do it better?
    >


    FAQ 5.8, I believe, should help to get closer to the solution.

    V
    Victor Bazarov, Dec 6, 2005
    #5
  6. JoeC

    JoeC Guest

    thanks..
    JoeC, Dec 6, 2005
    #6
  7. JoeC

    JoeC Guest

    I got help with now problem. It was pretty basic.

    But box.push_back(ms); Is still viving me an error. It says that
    ms is not declared.
    I have still to look up they syntax of the switch/case part

    42 C:\Documents and Settings\Owner.ROOT\My
    Documents\C++\EngineII\action.cpp `ms' undeclared (first use this
    function)
    JoeC, Dec 6, 2005
    #7
  8. JoeC

    JoeC Guest

    Thanks, I got that but still the push back part is not working it is
    saying that ms is not declared.
    JoeC, Dec 6, 2005
    #8
  9. JoeC

    Howard Guest

    "JoeC" <> wrote in message
    news:...
    >I got help with now problem. It was pretty basic.
    >
    > But box.push_back(ms); Is still viving me an error. It says that
    > ms is not declared.
    > I have still to look up they syntax of the switch/case part
    >
    > 42 C:\Documents and Settings\Owner.ROOT\My
    > Documents\C++\EngineII\action.cpp `ms' undeclared (first use this
    > function)
    >


    Judging from the indentation in the incomplete code you posted, I suspect
    you're inside some kind of loop or switch statement when ms is declared.
    Instead of declaring it inside that block, you could declare it before the
    block. Or perhaps, instead, you need to move the push_back to immediately
    after the line where you declare the ms variable (and create its instance)?
    But we can't tell from your sparse example which approach is appropriate.

    -Howard
    Howard, Dec 6, 2005
    #9
  10. JoeC

    Ben Pope Guest

    JoeC wrote:
    >
    > vector<Sprite*>box;
    > ++++++++++++++++++++++++++++++++++
    > tank = new Bitmap(hdc, IDB_IMAGE2, g_hin);
    > Sprite * ms = new Sprite(tank, rcBounds, BA_BOUNCE);
    > break;
    >
    > box.push_back(ms);
    >
    > +++++++++++++++++++++++++++++++++++++++
    >
    > for(vector<Sprite*>::iterator itr = box.begin();
    > itr != box.end(); itr++){
    > itr->Update();
    > }
    >
    > This isn't working how can I do it better?


    What's Sprite?

    Looks like sprite is a pointer, and your iterator is a "pointer" so
    you'll need to dereference twice.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...
    Ben Pope, Dec 6, 2005
    #10
  11. JoeC

    JoeC Guest

    A Sprite is a pointer, it dosn't matter what else, it is somthing that
    moves graphics and the Lib work. The last problem I am having with my
    code is that I want to put these sprite pointers in a vector but I get
    an error ms is not defined.

    109 C:\Documents and Settings\Owner.ROOT\My
    Documents\C++\EngineII\lib.cpp `ms' undeclared (first use this
    function)

    vector<Sprite*>box;

    ++++++++++++++++++++++++++++++++++++++++

    if(num == 6){
    ball = new Bitmap(hdc, IDB_IMAGE5, g_hin);
    Sprite *ms = new Sprite(ball, rcBounds, BA_BOUNCE);
    }

    box.push_back(ms); <-- error here.

    What am I doing wrong?
    JoeC, Dec 7, 2005
    #11
  12. On 7 Dec 2005 09:49:25 -0800, "JoeC" <> wrote:

    >A Sprite is a pointer, it dosn't matter what else, it is somthing that
    >moves graphics and the Lib work. The last problem I am having with my
    >code is that I want to put these sprite pointers in a vector but I get
    >an error ms is not defined.
    >
    >109 C:\Documents and Settings\Owner.ROOT\My
    >Documents\C++\EngineII\lib.cpp `ms' undeclared (first use this
    >function)
    >
    >vector<Sprite*>box;
    >
    >++++++++++++++++++++++++++++++++++++++++
    >
    > if(num == 6){
    > ball = new Bitmap(hdc, IDB_IMAGE5, g_hin);
    > Sprite *ms = new Sprite(ball, rcBounds, BA_BOUNCE);
    > }
    >
    > box.push_back(ms); <-- error here.
    >
    >What am I doing wrong?


    ms is only valid inside the braces of your if() statement. After the
    closing brace, it goes out of scope.

    Most likely, you will want to move the vector::push_back call inside
    of your if() scope.

    --
    Bob Hairgrove
    Bob Hairgrove, Dec 7, 2005
    #12
  13. JoeC

    JoeC Guest

    Yeh, I realized that I am trying to get this program to run, it
    compiles but it is a challenge getting the sprites to show up.
    JoeC, Dec 7, 2005
    #13
  14. JoeC

    Howard Guest

    "JoeC" <> wrote in message
    news:...
    > Yeh, I realized that I am trying to get this program to run, it
    > compiles but it is a challenge getting the sprites to show up.
    >


    Do you have a C++ question?

    -Howard
    Howard, Dec 7, 2005
    #14
  15. JoeC

    JoeC Guest

    Yes, and it was answered or I figureed out what I wanted to do.
    JoeC, Dec 7, 2005
    #15
    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. Mark Probert
    Replies:
    8
    Views:
    306
    Mark Probert
    Apr 13, 2005
  2. kelvSYC
    Replies:
    1
    Views:
    697
    Axter
    May 18, 2005
  3. Replies:
    3
    Views:
    680
    Shadowman
    Mar 26, 2008
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    656
  5. Guest
    Replies:
    0
    Views:
    426
    Guest
    Sep 14, 2005
Loading...

Share This Page