Question about pointers and vectors

J

JoeC

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?
 
A

Andre Kostur

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();
 
M

Mike Wahler

JoeC said:
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
 
B

Bob Hairgrove

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.
 
V

Victor Bazarov

JoeC said:
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
 
J

JoeC

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)
 
J

JoeC

Thanks, I got that but still the push back part is not working it is
saying that ms is not declared.
 
H

Howard

JoeC said:
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
 
B

Ben Pope

JoeC said:
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
 
J

JoeC

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?
 
B

Bob Hairgrove

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.
 
J

JoeC

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.
 
H

Howard

JoeC said:
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
 

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

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top