473,414 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

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
Jul 22 '05 #1
4 1193
"matobinder" <ma********@hotmail.com> wrote in message
news:61**************************@posting.google.c om...
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.
Jul 22 '05 #2

"matobinder" <ma********@hotmail.com> wrote in message
news:61**************************@posting.google.c om...
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
Jul 22 '05 #3
ma********@hotmail.com (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.
Jul 22 '05 #4
>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
Jul 22 '05 #5

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

Similar topics

2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
5
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
14
by: Gama Franco | last post by:
Hi, I'm designing an interface for a shared library, and I would like to know if there is a standard about how to return an object to the user. I will use exceptions to report errors, so there...
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
2
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
6
by: aaragon | last post by:
Hi, I'm designing a Matrix class so I read what Bjarne Stroustrup has on section 22.4.6 about it. It shows that it is possible to eliminate the temporaries by delaying the construction of the...
3
by: NewLine | last post by:
Hi If I have a function: std::vector<intmy_rbv() { std::vector<intx; // some stuff return x; }
3
by: massysett | last post by:
Greetings, Having classes with member objects that have STL containers of objects whose definitions are incomplete results in undefined behavior. See for example: ...
4
by: fabian.lim | last post by:
Hi All, Im a newbie to C++, I am trying to customize the vector template STL to a template Class. My code is shown below. Im confused about something and maybe somebody here might be able to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.