[try Beta version]
Not logged in

 
return types

Pages: 12
Mar 25, 2014 at 11:43pm
Ok I have a class called items and stored in a vector as vector<items*> item.

now I want to make a function to return the placement # that the item is stored at with a unsigned int. but if I don't find the item is there like a null unsigned int that can be returned.
1
2
3
4
5
6
7
8
9
10
11
12
unsigned int someFunc(vector<items*> item)
{
   unsigned int size = item.size();
   for (unsigned int i = 0; i < size; ++i)
   {
       if (item[i]->getItemName() == "sword")
       {
        return i;
       }
   }
//i need code here incase the item is not there 
}


because the item might not be there and i would like to do a line
if(unsigned int) do this code
Mar 25, 2014 at 11:46pm
return -1 maybe?
Mar 25, 2014 at 11:51pm
i was thinking about that, but -1 is that false? i thought only 0 is false.
might be what i have to do though.
Mar 25, 2014 at 11:52pm
yes -1 is considered false. i thought the same thing as you. however i was taught that < 1 is false and everything else is true.
Mar 25, 2014 at 11:55pm
i just read that 0 is false and everything else -1 to -#### and 1 to #### are true but that null is false, i guess i will have to try see if i can return null through a unsigned int
Mar 25, 2014 at 11:56pm
You could also add a reference to a bool in your function parameters, which you would manipulate within the function.

Edit:
or better yet, make the return type 'bool', and the 'int' the reference.
Last edited on Mar 25, 2014 at 11:57pm
Mar 25, 2014 at 11:58pm
The std::find function already does what you want. It returns an iterator, so if it can't find the item, then the iterator will be equal to the vector's end() iterator.
Mar 26, 2014 at 12:05am
yes -1 is considered false

That is not correct. 0 is false and anything else is considered true. Many tend to use 0 and 1 though. Anyways you can not return -1 when it is unsigned. If you try this you will get something near
4294967295
if int is by default a long.
Mar 26, 2014 at 12:06am
@Little Bobby Tables
How about no?


As to this problem, it can depend. Do you expect not finding the item normal, or is this just for debugging purposes? In the case you don't find it, what you were supposed to do with it?

I've had this situation before with a few things. Sometimes what you can do is just declare a single dummy object, that can be returned but not actually effect what the program does in any way.

In the case the error is not normal, you may also want to just shut things down.

Typically best solution however is have it designed so that you can know something exists in the first place before you try to use it.
Mar 26, 2014 at 12:12am
-1 always works for me when i write c++ code.
@Little Bobby Tables
How about no?

thats a bit rude.


Typically best solution however is have it designed so that you can know something exists in the first place before you try to use it.

how about no? there are many solutions preferable to that

Anyways you can not return -1 when it is unsigned

sorry i was mixing up unsigned and signed
Mar 26, 2014 at 12:19am
Austin J (326) yes i expect to find the item so i guess can i make a unsigned int a int? that might work easier if i can do that.
Mar 26, 2014 at 12:22am
Converting to signed(should be default with int) would let you return -1 as an error code yes.
Mar 26, 2014 at 1:01am
If not finding an item is a problem, why not throw an exception instead?

@Little Bobby Tables
1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
    if (-1)
        std::cout << "-1 is True";
    if (0)
        std::cout << "0 is True";
    return 0;
}
-1 is True

http://ideone.com/M0eGH5
Last edited on Mar 26, 2014 at 1:02am
Mar 26, 2014 at 1:05am
yes i can see that, but on my system i use -1 for false and it has always worked fine for me
Mar 26, 2014 at 1:11am
Then your system is non-standard. "It has always worked fine for me" is never an excuse.


Also, I feel like my advice with iterators is being completely ignored. Iterators solve this problem quite nicely with needing to resort to violent things like exceptions.
Mar 26, 2014 at 1:42am
@LB
Ah, I must have skipped over your post. No, you are absolutely right: In this context (especially accessing from an STL container), iterators do exactly what you need through std::find.
Mar 26, 2014 at 2:33am
thanks for all the replys and i went with a int as the return as i think it was less typing the the .find() function which im not as used to. I should probably learn the .find() more as im sure it will come in handy as well.
Mar 26, 2014 at 6:02am
L B , I'm with you on this. We seem to both favor actually using STL to a fuller extent :p

@
Little Bobby Tables

I responded such because not only was that a bad answer, you said it in a way that seemed quite arrogant.

That's very opinionated, and you've not contributed any solution to this. Try using your idea in a metadata system, or interacting with a player's inventory.
Mar 26, 2014 at 1:54pm
sorry i was mixing up unsigned and signed

The clue's in the name... :P
Mar 26, 2014 at 2:19pm
> I want to make a function to return the placement # that the item is stored at with a unsigned int.

>> return -1 maybe?

Yes.

AFAIK, returning -1 (for an invalid position in a random access sequence) is perfectly acceptable. The valid positions are [0, N-1]. We could return any value outside that range to indicate failure to find.

There is nothing wrong in interpreting -1 as an unsigned integer; doing so is mainstream C++. For instance:

std::basic_string<>::size_type is an unsigned integral type (commonly std::size_t)

And the class has: static const size_type npos = -1 ;

Which is returned by the find functions if a substring or character is not found.
Pages: 12