return value from object in a vector

M

matobinder

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
 
A

Andrew Koenig

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

Paul

matobinder said:
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
 
O

Old Wolf

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

matobinder

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
 

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
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top