Signed/Unsigned mistmatch warning, Best way to fix?

Started by
5 comments, last by Jaiminho 16 years, 11 months ago
Hi, Got some simple code,

void app_DisplayAllMembers(const vector<Member*> &o_MemberList)
{
	for(int i = 0; i < o_MemberList.size(); i++)
	{
		o_MemberList->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
Advertisement
No. Just declare your for loop counter as an unsigned int:

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


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

-me
Fixed, and thanks for quick reply.

Malal
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->printDetails();	}


EDIT: I see you've come to our side in the end [smile]
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();	}}
Quote:Original post by ToohrVyk
	for(std::size_t i = 0; i < o_MemberList.size(); ++i)	{		o_MemberList->printDetails();	}

As if! I think you mean:
	for(std::vector<Member*>::size_type i = 0; i < o_MemberList.size(); ++i)	{		o_MemberList->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.
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.

This topic is closed to new replies.

Advertisement