return value from object in a vector



 DEVELOP > c-Plus-Plus > return value from object in a vector

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "matobinder"
Date: 12 Sep 2004 02:45:55 PM
Object: return value from object in a vector
Probably a simple problem here. I fairly new to STL, but getting to like
its features.
I have a vector of objects, due to nature of the objects, I need to have it
be a vector of pointers. I am handling the deallocation of those
objects myself.
So my problem is, lets say I have:
vector<MyClass*> thisGuy;
MyClass has a member function that returns a std::string, lets call
it MyClass::getName()
I am trying to do something like the following(assume getName really
returns joeblow).
if ( thisGuy.at(1)->getName() == "joeblow")
This compiles, but doesn't work as I expect, it always yeilds true, even
when it is not.
But if I do:
std::string buf = thisGuy.at(1)->getName();
if ( buf == "joeblow" )
This works as I expect. buf definitely is "joeblow".
Anyways, is there a way I can do the above comparison, without needing
to have the temporary std::string buf?
-mato
.

User: "Paul"

Title: Re: return value from object in a vector 12 Sep 2004 09:24:58 PM
"matobinder" <matobinder@hotmail.com> wrote in message
news:6159f3d0.0409121145.5602de1f@posting.google.com...

if ( thisGuy.at(1)->getName() == "joeblow")

This compiles, but doesn't work as I expect, it always yeilds true, even
when it is not.

Do you have an erroneous semicolon at the end of the if() statement?
Something like this:
if ( thisGuy.at(1)->getName() == "joeblow"); //<--Semicolon
This is usually the problem when an "if statement always returns true".
Paul
.

User: "Andrew Koenig"

Title: Re: return value from object in a vector 12 Sep 2004 02:49:39 PM
"matobinder" <matobinder@hotmail.com> wrote in message
news:6159f3d0.0409121145.5602de1f@posting.google.com...

I am trying to do something like the following(assume getName really
returns joeblow).

if ( thisGuy.at(1)->getName() == "joeblow")

This compiles, but doesn't work as I expect, it always yeilds true, even
when it is not.

It shouldn't misbehave that way. Something is wrong.

But if I do:

std::string buf = thisGuy.at(1)->getName();
if ( buf == "joeblow" )

This works as I expect. buf definitely is "joeblow".

If these two pieces of code different differently, you haven't told us the
whole story--there's a bug somewhere in code you haven't shown us.
Please take your program and cut it down to a short (less than 20 lines if
possible), complete program that exhibits the problem. Then post that
program here and we'll take a look at it.
.

User: "Old Wolf"

Title: Re: return value from object in a vector 13 Sep 2004 01:03:57 AM
(matobinder) wrote:


So my problem is, lets say I have:

vector<MyClass*> thisGuy;

MyClass has a member function that returns a std::string, lets call
it MyClass::getName()

I am trying to do something like the following(assume getName really
returns joeblow).

if ( thisGuy.at(1)->getName() == "joeblow")

Note, this is the second item in the vector (the first was thisGuy.at(0))


This compiles, but doesn't work as I expect, it always yeilds true, even
when it is not.

Something is seriously wrong there.. the only other likely error
I can think of would be if getName() returned a (char const *) and
the comparison was always false

But if I do:

std::string buf = thisGuy.at(1)->getName();
if ( buf == "joeblow" )

This works as I expect. buf definitely is "joeblow".

Anyways, is there a way I can do the above comparison, without needing
to have the temporary std::string buf?

if ( buf == std::string("joeblow") )
See if you can post a minimal compilable example that demonstrates
the problem. Also, what version of what compiler do you have.
.
User: "matobinder"

Title: Re: return value from object in a vector 13 Sep 2004 10:22:11 AM

I am trying to do something like the following(assume getName really
returns joeblow).

if ( thisGuy.at(1)->getName() == "joeblow")

This compiles, but doesn't work as I expect, it always yeilds true, even
when it is not.

But if I do:

std::string buf = thisGuy.at(1)->getName();
if ( buf == "joeblow" )

This works as I expect. buf definitely is "joeblow".

Arg... this is what I get for working way too many hours straight with out a
day off in a long time. It wasn't compiler issue or anything I'm just a
moron. My actual code was doing something like:
if (thisGuy.at(1)->getName() != "joeblow")
When I put the temp string in. I did the == instead.
I would have probably figured it out quicker, but for some odd reason, gdb
crashes my program when I try to print a value of the string in a vector.
g++ = 3.2.3
gdb = 6.2.1
i.e.
(gdb) print thisGuy.at(1)->getName()
Causes my app to segfault(not GDB) Not sure what the problem was, whenever I
can't use the debugger I really end up getting lost. I must have my debugger!
Especially after working 20+ hours straight.
I looked into the debugger issue more, I am getting close to a suspicion its
a bug in it. Writing up a test case to prove it and submit a bug if it
proves true
Thanks
-m
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER