Thread: Compiler Error: Unsigned vs Signed Integer Expression

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    38

    Compiler Error: Unsigned vs Signed Integer Expression

    RentalSystem.cpp:357: warning: comparison between signed and unsigned integer expressions

    I have a number of these errors in basically the same situation. Line 357 is :

    Code:
    for ( i = 0; i < m_vehicleVector.size(); i++ )
    m_vehicleVector is declared as :
    Code:
    vector<T> m_vehicleVector;
    It's declares as a private data member in a class template RentalSystem.cpp.
    Code:
    RentalSystem <NewCar> Rental
    With NewCar being another class. Now I'm new to templates, so I'd imagine I'm doing something wrong with my templates, but I can't see it. Any ideas?

    Thanks in advance,
    Chris

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    They are warnings, not errors... That said they should still be addressed. The problem is that i and the return type from the function call to the size member function are of different types. One is a signed type and one is an unsigned type. You can either make both signed or both unsigned depending on what they are supposed to do (probably unsigned in your case).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use vector<T>::size_type as the type for i.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    hk_mp5kpdw, my fault, they were warnings, not errors. I understand that the warning is telling me that it's trying to compare unsigned and signed ints. I tried declaring i as simply:
    Code:
    unsigned int i;
    but it didn't resolve the warnings. Also, daved, I'm not sure I understand the syntax of what you're trying to tell me to do. Sorry, could you clarify?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Use
    Code:
    vector<T>::size_type i;
    instead of
    Code:
    int i;
    because the type returned by vector.size() is vector<T>::size_type, which is just a typedef for an unsigned integral type. This way you won't have any mismatches, because they will be of the same type.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    38
    The compiler threw a warning at me for trying that.

    Code:
    RentalSystem.cpp: In member function `void
       RentalSystem<T>::ReadCommands(std::vector<type, std::allocator<type> >&,
       std::vector<assoc, std::allocator<assoc> >&, std::ifstream&)':
    RentalSystem.cpp:298: warning: `typename std::vector<T, std::allocator<_CharT>
       >::size_type' is implicitly a typename
    RentalSystem.cpp:298: warning: implicit typename is deprecated, please see the
       documentation for details

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So use:

    typename vector<T>::size_type i;
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cross platform portability, about data types...
    By gaah in forum C++ Programming
    Replies: 9
    Last Post: 01-21-2005, 10:32 PM
  2. sizeof and Expression Evaluation
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 08-11-2003, 07:11 PM
  3. All u wanted to know about data types&more
    By SAMSAM in forum Windows Programming
    Replies: 6
    Last Post: 03-11-2003, 03:22 PM
  4. Very large signed integer math question
    By Criz in forum C Programming
    Replies: 8
    Last Post: 12-01-2002, 12:43 PM