Warnings

Hello, I'm new to C++ and I would like if someone could tell me what warnings in Visual Studio 2008 are and why I should care? :)

I get alot of these:
warning C4018: '<' : signed/unsigned mismatch

What does that mean?
You have something like this:
1
2
3
4
signed int a = 5; // signed can be omitted
unsigned int b = 6;
if ( a < b ) // here is the warning, 'a' is signed and 'b' unsigned
    ...

For some values you may have problems but is not a big issue. If you can declare the values you are using to be both signed or unsigned. this would remove the warning ( You can also do nothing and keep the warning or compile disabling warnings )
Should I put signed or unsigned before every variable I create?

And what can go wrong if I don't?
Usually if you don't put anything you would get a signed variable. You should use signed or unsigned depending on what you need:
If you need a number which can have negative values, use a signed variable, if you need numbers which are always zero or positive and which can reach slightly high values, use an unsigned.
Usually unsigned integers are used as size types -a size can never be negative-
I find these errors are usually when dealing with standard containers and the .size() member function. For example:

for (int n = 0; n < myvec.size(); n++)

Since the vector::size_type is unsigned (at least on my compiler) I get those warnings.

One way to get rid of them is to use the size_type:

for (vector::size_type n = 0; n < myvec.size(); n++)

Another is to simply cast to the appropriate signedness:

for (unsigned n = 0; n < (unsigned)myvec.size(); n++)

Hope this helps.
Last edited on
Thanks for the answers, Bazzy :)
Topic archived. No new replies allowed.