By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

VECTOR problem

P: n/a
I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector
and pointed by *buyer ?
*
*at beginning ,I did it like this,I wrote a function for Buyer class.like
*below:
*
*Buyer* Buyer::return_address(void) {
*return this;
*}
*
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator).return_address()==(*buyer).return_ address())
*if it is true ,i will use buyer_queue.erase();to delete the element.
*
*but the if()statement never return true,why is that?????
*
*to sum up,how can I delete the element which that pointer pointed(by
only knowing/haveing that pointer)?
*============================================
*/
};

void main{

Buyer b=Buyer();
b.start();

}
Jul 22 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

"Joseph" <wu*******@hotmail.com> schrieb im Newsbeitrag
news:Xn**********************@203.96.216.21...
I was doing my assignment,but encountered a problem at last step!!!!!!
for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector and pointed by *buyer ?
*
*at beginning ,I did it like this,I wrote a function for Buyer class.like *below:
*
*Buyer* Buyer::return_address(void) {
*return this;
*}
*
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator).return_address()==(*buyer).return_ address())
*if it is true ,i will use buyer_queue.erase();to delete the element. *
*but the if()statement never return true,why is that?????
*
*to sum up,how can I delete the element which that pointer pointed(by only knowing/haveing that pointer)?
*============================================
*/
};

void main{

Buyer b=Buyer();
b.start();

}


Slowly, cowboy. If you allocate new objects with vector class, the
pointers to the elements might change when you add new elements or
remove some. Use a list<> if you want the pointer addresses to stay.
Next, you don't need a "return_address" function, since "&" does
exaclty this:
if (&*iterator == pointer_to_buyer) ...
If you only have the pointer to that element (why don't you have an
iterator to it!?) you would have to loop through the list (vector,
still?) and check if it's pointer addresses equal. Then you have an
iterator and can erase it. Again, adding/rmoving may change addresses
of _all_ container elements in a vector<> container! Use a list<>,
which keeps the addresses until destruction.
Best solution: Pass an iterator to that function.

HTH,

--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com





Jul 22 '05 #2

P: n/a
Joseph <wu*******@hotmail.com> wrote:
I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector
and pointed by *buyer ?
vector<Buyer>::iterator it = buyer_queue.begin();

while ( it != buyer_queue.end() || &*it != buyer ) {
++it;
}

if ( it != buyer_queue.end() ) {
// it is an iterator to the buyer in the vector
}
*but the if()statement never return true,why is that?????
Because the buyer you are passing into the function isn't in the vector.
My guess is, the problem you are having isn't in the code you posted
(which, while unconventional, should do what you want.) Your problem is
in thinking that the buyer passed into the vector is actually there.
Note your main function:
void main{ that should be 'int main() {'
Buyer b=Buyer(); the '=Buyer()' part isn't necessary.
b.start();

}


You created a Buyer object, but never added it to the vector. If you
did, I expect you did something like this:

int main() {
Buyer b;
buyer_queue.push_back(b);
b.start();
}

The above will not find the Buyer in the vector because 'b' isn't *in*
the vector, it's in the main function. If you do this:

int main() {
Buyer b;
buyer_queue.push_back(b);
buyer_queue[0].start();
}

then you will be calling start on an object that is *in* the vector.
Jul 22 '05 #3

P: n/a

"Joseph" <wu*******@hotmail.com> wrote in message
news:Xn**********************@203.96.216.21...
I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};
Why use a friend function, but pass it

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in vector
and pointed by *buyer ?
*
*at beginning ,I did it like this,I wrote a function for Buyer class.like
*below:
*
*Buyer* Buyer::return_address(void) {
*return this;
*}
*
No need for this function. A pointer itself holds the address of the
object. And for iterators you can just use &*it.
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator).return_address()==(*buyer).return_ address())
*if it is true ,i will use buyer_queue.erase();to delete the element.
*
*but the if()statement never return true,why is that?????
*
*to sum up,how can I delete the element which that pointer pointed(by
only knowing/haveing that pointer)?
*============================================
*/
};

void main{

Buyer b=Buyer();
Just "Buyer b;" is all you need here. Or did you intend "Buyer* b = new
Buyer();"?
b.start();

}


I think you've removed *far* too much. You don't push b onto the vector
anywhere, so it's not in the vector. And even if you did, why immediately
search for it and remove it? Simplification for posting is fine, but not at
the expense of the meaning of the code.

Are you sure you want to store the objects themselves in the vector? You
know, a vector makes a *copy* of the object when you call push_back, so the
address of the object in the vector wil *never* be the same as the original
object. If you need to use addresses to compare objects, then you need to
push pointers, not objects. (And in that case, just compare *it with the
pointer, instead of &*it.)

-Howard


Jul 22 '05 #4

P: n/a

"Howard" <al*****@hotmail.com> wrote in message
news:nE********************@bgtnsc04-news.ops.worldnet.att.net...

"Joseph" <wu*******@hotmail.com> wrote in message
news:Xn**********************@203.96.216.21...
I was doing my assignment,but encountered a problem at last step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};


Why use a friend function, but pass it


Oops! Forgot to finish that thought! :-) Never mind...I misunderstood what
I was reading, anyway. (The lack of indentation in the post fooled me.)

-H
Jul 22 '05 #5

P: n/a
"Daniel T." <po********@eathlink.net> wrote in
news:po******************************@news06.east. earthlink.net:
Joseph <wu*******@hotmail.com> wrote:
I was doing my assignment,but encountered a problem at last
step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in
vector and pointed by *buyer ?


vector<Buyer>::iterator it = buyer_queue.begin();

while ( it != buyer_queue.end() || &*it != buyer ) {
++it;
}

if ( it != buyer_queue.end() ) {
// it is an iterator to the buyer in the vector
}
*but the if()statement never return true,why is that?????


Because the buyer you are passing into the function isn't in the
vector. My guess is, the problem you are having isn't in the code you
posted (which, while unconventional, should do what you want.) Your
problem is in thinking that the buyer passed into the vector is
actually there. Note your main function:
void main{

that should be 'int main() {'

Buyer b=Buyer();

the '=Buyer()' part isn't necessary.
b.start();

}


You created a Buyer object, but never added it to the vector. If you
did, I expect you did something like this:

int main() {
Buyer b;
buyer_queue.push_back(b);
b.start();
}

The above will not find the Buyer in the vector because 'b' isn't *in*
the vector, it's in the main function. If you do this:

int main() {
Buyer b;
buyer_queue.push_back(b);
buyer_queue[0].start();
}

then you will be calling start on an object that is *in* the vector.


oh,actually I did buyer_queue.push_back(b);

so what you mean is ,when I call .start() ,it is actually called in main
(),which its address differs from its address in the vector?

I think I should consider use list as Gernot suggested,any way,lots thank
to u 2.!!!!!!
Jul 22 '05 #6

P: n/a
JosephWu <wu*******@hotmail.com> wrote:
"Daniel T." <po********@eathlink.net> wrote:
Joseph <wu*******@hotmail.com> wrote:
I was doing my assignment,but encountered a problem at last
step!!!!!!

for easy reading, i ommited lots of other things

//=====================code begin================================
class Buyer{
void start(void);
friend void Buyer_run(Buyer *buyer);
};
void Buyer::start(void){
Buyer_run(this);
};

std::vector<Buyer> buyer_queue;

void Buyer::Buyer_run(Buyer* buyer){
/*========================================
*my problem is here.how can I find out the object which stored in
vector and pointed by *buyer ?


vector<Buyer>::iterator it = buyer_queue.begin();

while ( it != buyer_queue.end() || &*it != buyer ) {
++it;
}

if ( it != buyer_queue.end() ) {
// it is an iterator to the buyer in the vector
}
*but the if()statement never return true,why is that?????


Because the buyer you are passing into the function isn't in the
vector. My guess is, the problem you are having isn't in the code you
posted (which, while unconventional, should do what you want.) Your
problem is in thinking that the buyer passed into the vector is
actually there. Note your main function:
void main{

that should be 'int main() {'

Buyer b=Buyer();

the '=Buyer()' part isn't necessary.
b.start();

}


You created a Buyer object, but never added it to the vector. If you
did, I expect you did something like this:

int main() {
Buyer b;
buyer_queue.push_back(b);
b.start();
}

The above will not find the Buyer in the vector because 'b' isn't *in*
the vector, it's in the main function. If you do this:

int main() {
Buyer b;
buyer_queue.push_back(b);
buyer_queue[0].start();
}

then you will be calling start on an object that is *in* the vector.


oh,actually I did buyer_queue.push_back(b);

so what you mean is ,when I call .start() ,it is actually called in main
(),which its address differs from its address in the vector?


No. When you did buyer_queue.push_back(b) a *copy* of 'b' was placed in
the buyer_queue vector. So when you call b.start() you find that 'b'
itself is *not* in the vector (a copy is instead.)
Jul 22 '05 #7

P: n/a
>
oh,actually I did buyer_queue.push_back(b);

so what you mean is ,when I call .start() ,it is actually called in main
(),which its address differs from its address in the vector?

I think I should consider use list as Gernot suggested,any way,lots thank
to u 2.!!!!!!


If I'm not mistaken, list will make a copy, too, so the address is still
going to be different. But without more code, I can't tell what you're
*really* trying to do, so I can't tell if storing pointers will solve the
problem or not.

-Howard
Jul 22 '05 #8

P: n/a
"Howard" <al*****@hotmail.com> wrote in
news:NI********************@bgtnsc05-news.ops.worldnet.att.net:

"Howard" <al*****@hotmail.com> wrote in message
news:nE********************@bgtnsc04-news.ops.worldnet.att.net...

"Joseph" <wu*******@hotmail.com> wrote in message
news:Xn**********************@203.96.216.21...
> I was doing my assignment,but encountered a problem at last
> step!!!!!!
>
>
>
> for easy reading, i ommited lots of other things
>
> //=====================code begin================================
> class Buyer{
> void start(void);
> friend void Buyer_run(Buyer *buyer);
> };
> void Buyer::start(void){
> Buyer_run(this);
> };


Why use a friend function, but pass it


Oops! Forgot to finish that thought! :-) Never mind...I
misunderstood what I was reading, anyway. (The lack of indentation in
the post fooled me.)

-H


thank u Howard,I will either use list or do like what u said: to push
pointer into vector.
Jul 22 '05 #9

P: n/a
"Howard" <al*****@hotmail.com> wrote:

oh,actually I did buyer_queue.push_back(b);

so what you mean is ,when I call .start() ,it is actually called in main
(),which its address differs from its address in the vector?

I think I should consider use list as Gernot suggested,any way,lots thank
to u 2.!!!!!!


If I'm not mistaken, list will make a copy, too, so the address is still
going to be different. But without more code, I can't tell what you're
*really* trying to do, so I can't tell if storing pointers will solve the
problem or not.


He's not, look at the code in his OP.
Jul 22 '05 #10

P: n/a

"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news01.east. earthlink.net...
"Howard" <al*****@hotmail.com> wrote:

oh,actually I did buyer_queue.push_back(b);

so what you mean is ,when I call .start() ,it is actually called in main (),which its address differs from its address in the vector?

I think I should consider use list as Gernot suggested,any way,lots thank to u 2.!!!!!!


If I'm not mistaken, list will make a copy, too, so the address is still
going to be different. But without more code, I can't tell what you're
*really* trying to do, so I can't tell if storing pointers will solve the problem or not.


He's not, look at the code in his OP.


I understand that he's not *currently* storing pointers. I was questioning
whether *changing* to using pointers would solve his problem. I suspect
that it will, but there's simply not enough info to know for sure, because
he removed too much before posting.

-Howard

Jul 22 '05 #11

P: n/a
"Howard" <al*****@hotmail.com> wrote:
"Daniel T." <po********@eathlink.net> wrote:
"Howard" <al*****@hotmail.com> wrote:
>
> oh,actually I did buyer_queue.push_back(b);
>
> so what you mean is ,when I call .start() ,it is actually called in
> main
> (),which its address differs from its address in the vector?
>
> I think I should consider use list as Gernot suggested,any way,lots
> thank
> to u 2.!!!!!!

If I'm not mistaken, list will make a copy, too, so the address is still
going to be different. But without more code, I can't tell what you're
*really* trying to do, so I can't tell if storing pointers will solve
the
problem or not.


He's not, look at the code in his OP.


I understand that he's not *currently* storing pointers. I was questioning
whether *changing* to using pointers would solve his problem. I suspect
that it will, but there's simply not enough info to know for sure, because
he removed too much before posting.


OK, I see that you are approaching the problem from a different angle
than I am. Under what context would it be appropriate to store pointers
in the vector? The only contexts I can think of off hand are if the
class is meant to be derived from, or if the vector is supposed to store
every object created from this class (which would mean that the
push_back would be in the class' c_tors and not in main...
Jul 22 '05 #12

P: n/a
> OK, I see that you are approaching the problem from a different
angle
than I am. Under what context would it be appropriate to store pointers in the vector? The only contexts I can think of off hand are if the
class is meant to be derived from, or if the vector is supposed to store every object created from this class (which would mean that the
push_back would be in the class' c_tors and not in main...


Storing pointers is very dangerous. You have to make very sure, that
the pointer points to a valid object. It's better to make a class that
handles pointers to something and has a flag: Is valid pointer, which
will be reset if you delete the object itself.

I'm quite regularly store pointers to objects for fast iteration of
tree structures or for referencing a list of nodes, where you only
want those with x=0 e.g. But only for temporary use. I never store
this list in memory outside the function that needs it.

HTH,
Gernot
Jul 22 '05 #13

P: n/a

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2o************@uni-berlin.de...
OK, I see that you are approaching the problem from a different

angle
than I am. Under what context would it be appropriate to store

pointers
in the vector? The only contexts I can think of off hand are if the
class is meant to be derived from, or if the vector is supposed to

store
every object created from this class (which would mean that the
push_back would be in the class' c_tors and not in main...


Storing pointers is very dangerous. You have to make very sure, that
the pointer points to a valid object. It's better to make a class that
handles pointers to something and has a flag: Is valid pointer, which
will be reset if you delete the object itself.

I'm quite regularly store pointers to objects for fast iteration of
tree structures or for referencing a list of nodes, where you only
want those with x=0 e.g. But only for temporary use. I never store
this list in memory outside the function that needs it.

HTH,
Gernot


As was mentioned, using pointers allows you to store objects that all belong
to the same inheritance hierarchy. That's a very common practice. It also
can improve performance by not requiring copies of objects to be made,
especially when there are a lot of inserts and deletes to be made.

Storing pointers in a vector is no more dangerous than using pointers
anywhere else. The same rule applies: it is undefined behavior to access a
pointer which does not point to a valid object. It comes down to designing
the "ownership" of the pointer properly, so that you're never left guessing
whose responsibility it is to manage the object pointed to. (I think that
"smart" pointers help in this regard, although I haven't used them myself.)

But the OP's problem was that he's *comparing* pointers (addresses). But
where do the pointers (outside the vector) come from? He doesn't show the
relevant code...just a non-working two-line main function.

As I said before, when calling push_back, a *copy* is made. So, unless you
somehow get the address from the vector where that copy was made in order to
later compare against it, the comparison will *always* fail. Which leads to
just a few choices: the design is basically flawed; or the vector should
hold copies of pointers, and the comparison should compare those copies
(leading to the problem of who "owns" the object pointed to); or (possibly
the better choice) use some member data of the object to identify it (NOT
its address!), such as an ID field.

The poster has not returned to elaborate on the original problem, so
anything is pure speculation at this point.

-Howard
Jul 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.