473,769 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VECTOR problem

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(vo id){
Buyer_run(this) ;
};

std::vector<Buy er> buyer_queue;

void Buyer::Buyer_ru n(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_a ddress(void) {
*return this;
*}
*
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator) .return_address ()==(*buyer).re turn_address())
*if it is true ,i will use buyer_queue.era se();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
13 2150

"Joseph" <wu*******@hotm ail.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(vo id){
Buyer_run(this) ;
};

std::vector<Buy er> buyer_queue;

void Buyer::Buyer_ru n(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_a ddress(void) {
*return this;
*}
*
*and then use iterator to loop the vector,after that
*I do it like this-->
*if((*iterator) .return_address ()==(*buyer).re turn_address())
*if it is true ,i will use buyer_queue.era se();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_buye r) ...
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%c gl%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
Joseph <wu*******@hotm ail.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(vo id){
Buyer_run(this) ;
};

std::vector<Buy er> buyer_queue;

void Buyer::Buyer_ru n(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.beg in();

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.pus h_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.pus h_back(b);
buyer_queue[0].start();
}

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

"Joseph" <wu*******@hotm ail.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(vo id){
Buyer_run(this) ;
};
Why use a friend function, but pass it

std::vector<Buy er> buyer_queue;

void Buyer::Buyer_ru n(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_a ddress(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).re turn_address())
*if it is true ,i will use buyer_queue.era se();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

"Howard" <al*****@hotmai l.com> wrote in message
news:nE******** ************@bg tnsc04-news.ops.worldn et.att.net...

"Joseph" <wu*******@hotm ail.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(vo id){
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
"Daniel T." <po********@eat hlink.net> wrote in
news:po******** *************** *******@news06. east.earthlink. net:
Joseph <wu*******@hotm ail.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(vo id){
Buyer_run(this) ;
};

std::vector<Buy er> buyer_queue;

void Buyer::Buyer_ru n(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.beg in();

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.pus h_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.pus h_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.pus h_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
JosephWu <wu*******@hotm ail.com> wrote:
"Daniel T." <po********@eat hlink.net> wrote:
Joseph <wu*******@hotm ail.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(vo id){
Buyer_run(this) ;
};

std::vector<Buy er> buyer_queue;

void Buyer::Buyer_ru n(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.beg in();

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.pus h_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.pus h_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.pus h_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.pus h_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
>
oh,actually I did buyer_queue.pus h_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
"Howard" <al*****@hotmai l.com> wrote in
news:NI******** ************@bg tnsc05-news.ops.worldn et.att.net:

"Howard" <al*****@hotmai l.com> wrote in message
news:nE******** ************@bg tnsc04-news.ops.worldn et.att.net...

"Joseph" <wu*******@hotm ail.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(vo id){
> 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
"Howard" <al*****@hotmai l.com> wrote:

oh,actually I did buyer_queue.pus h_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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
10629
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using namespace std; : : vector<string>* getTyphoon()
11
2906
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
13
2355
by: Steve | last post by:
I have defined the following private object: std::vector<Banana> bananas; in my header file. I have also added a method called FillVector(), which sets the size of the vector and fills it with Banana objects. In another method (getSecondBanana()) I want to access the contents of bananas, using e.g.
18
2576
by: imutate | last post by:
I have an integer variable and I am testing it as follows #define NULL_VAL -1 ... if (x.id != NULL_VAL) { std::cout << x.id << std::endl; ... }
0
2142
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition the vector of vectors according to the median value of one the dimensions. I tried to use the stl partition method like this: // S is a vector of vectors containing the points vector < vector <int> >::iterator iter;
11
1922
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The destruction of the assigned memory seems to call the class destructor more than once. I don't know the reason or whether I used the vector class correctly. Attached is my program. Thanks for your help. Regards,
9
3759
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
6
4002
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std; class super{ protected:
4
1926
by: shuisheng | last post by:
Dear All, I have a question. Assume #include <vector> using namespace std; class A { private:
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.