Sign in to follow this  
Malal

Signed/Unsigned mistmatch warning, Best way to fix?

Recommended Posts

Malal    118
Hi, Got some simple code,
void app_DisplayAllMembers(const vector<Member*> &o_MemberList)
{
	for(int i = 0; i < o_MemberList.size(); i++)
	{
		o_MemberList[i]->printDetails();
	}
}

I'm using Visual Studio 2005 and am getting the following warning 'warning C4018: '<' : signed/unsigned mismatch' Whats the best way to get rid of this? Should i cast o_MemberList.size() to an int? or is there a better way? Thanks Malal

Share this post


Link to post
Share on other sites
Palidine    1315
No. Just declare your for loop counter as an unsigned int:


for(unsigned int i = 0; i < o_MemberList.size(); ++i)
{
o_MemberList[i]->printDetails();
}




or just declare it as a size_t which is what size() returns

-me

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by Palidine
No. Just declare your for loop counter as an unsigned int:

*** Source Snippet Removed ***


No. Just declare your for loop counter as the correct type for indexing:


for(std::size_t i = 0; i < o_MemberList.size(); ++i)
{
o_MemberList[i]->printDetails();
}




EDIT: I see you've come to our side in the end [smile]

Share this post


Link to post
Share on other sites
Palidine    1315
Quote:
Original post by ToohrVyk
No. Just declare your for loop counter as the correct type for indexing:


=)

however, since you're using the stl, you should iterate using the stl:


void app_DisplayAllMembers(const vector<Member*> &o_MemberList)
{
vector<Member*>::iterator ite = o_MemberList.end();
for(vector<Member*>::iterator it = o_MemberList.begin(); it != ite; ++it )
{
(*it)->printDetails();
}
}



Share this post


Link to post
Share on other sites
Sneftel    1788
Quote:
Original post by ToohrVyk

for(std::size_t i = 0; i < o_MemberList.size(); ++i)
{
o_MemberList[i]->printDetails();
}


As if! I think you mean:

for(std::vector<Member*>::size_type i = 0; i < o_MemberList.size(); ++i)
{
o_MemberList[i]->printDetails();
}



Of course, all of this is manifestly stupid. Passing a negative index into the function is going to screw stuff up in exactly the same way as passing in a really, really big index. The basic problem is that some people use unsigned integers as a method of bounding domains, and some people don't. Unsigned integers are crap at this, by the way, which is why I'd suggest just using ints and disabling the warnings. I'm all for warning-free code, but unsigned gains you precisely nothing here.

Share this post


Link to post
Share on other sites
Jaiminho    184
Quote:
Original post by Sneftel
Of course, all of this is manifestly stupid. Passing a negative index into the function is going to screw stuff up in exactly the same way as passing in a really, really big index. The basic problem is that some people use unsigned integers as a method of bounding domains, and some people don't. Unsigned integers are crap at this, by the way, which is why I'd suggest just using ints and disabling the warnings. I'm all for warning-free code, but unsigned gains you precisely nothing here.

I always thought about that the same way. Simply stupid to restrict iteration to unsigned data. It also restricts reverse iteration, since you can't get that -1 to stop the loop. In the other hand, warnings are always nice... except for this one.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this