Making types the same..

This is a discussion on Making types the same.. within the C++ Programming forums, part of the General Programming Boards category; I am playing around with C++ again, and I have the following line which is causing a warning from g++: ...

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528

    Making types the same..

    I am playing around with C++ again, and I have the following line which is causing a warning from g++:

    Code:
    vector<string> v;
    ...
    for(int i = 0; i < v.size(); i++) {
    The warning is:
    Code:
    vec.cpp: In function ‘int main()’:
    vec.cpp:16: warning: comparison between signed and unsigned integer expressions
    What do I do to satisfy the compiler? I assume I would declare i in some way connected v?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by kermit
    What do I do to satisfy the compiler? I assume I would declare i in some way connected v?
    Write:
    Code:
    for(vector<string>::size_type i = 0; i < v.size(); i++) {
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Perfect, thanks. I am assuming that as the text I am using progresses, it will cover this sort of thing.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Unlikely, if it hasn't done it so far...

    Even using a simple unsigned might be enough to silence the computer and be more correct (although perhaps it can fail if sizeof(std::vector<T>::size_type) > sizeof(unsigned) and v.size() > std::numeric_limits<unsigned>::max() )

    Iterators would be another good choice for iterating over a container, and if possible std::for_each might be even better (or BOOST_FOREACH and similar).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by anon View Post
    Unlikely, if it hasn't done it so far...
    Perhaps I will switch texts then. The one I am currently using is Eckel's Thinking in C++. I am in possession of another one (C++ Primer) that I have heard good things about.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    Quote Originally Posted by kermit
    Perhaps I will switch texts then. The one I am currently using is Eckel's Thinking in C++. I am in possession of another one (C++ Primer) that I have heard good things about.
    I would recommend Accelerated C++ given that you already have some background.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Thanks, I will check it out.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Thinking in C++ actually seems quite OK. He actually seems to use size_t generally for such loops (if I'm not mistaken Container::size_type is a typedef of Allocator::size_type which in turn is supposed to be the typedef for std::size_t.

    --The typedef members pointer, const_pointer, size_type, and differ-
    ence_type are required to be T*, T const*, size_t, and ptrdiff_t,
    respectively.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's only guaranteed for the default allocator. But I've never heard of an allocator that does it differently.
    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

  10. #10
    Registered User bboozzoo's Avatar
    Join Date
    Jan 2009
    Posts
    14
    being extra lazy you can always use std::for_each from <algorithm> or if you need to break the loop explicitly at some point (and writing C++ code, you're likely to have Boost laying around) BOOST_FOREACH might work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making sprites
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 02-20-2010, 07:00 AM
  2. Generalising methods to handle multiple weapon types
    By Swarvy in forum Game Programming
    Replies: 2
    Last Post: 05-22-2009, 03:52 AM
  3. What types of files can C++ file processing handle?
    By darsunt in forum C++ Programming
    Replies: 9
    Last Post: 10-28-2008, 12:33 PM
  4. Making great graphics
    By MadCow257 in forum Game Programming
    Replies: 1
    Last Post: 02-20-2006, 11:59 PM
  5. Making control...
    By Finchie_88 in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2004, 02:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21