Think in C++ book example not compilling

This is a discussion on Think in C++ book example not compilling within the C++ Programming forums, part of the General Programming Boards category; oki folks I got some doubts about he following code: Code: //Creating a vector that holds integers #include <iostream> #include ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Think in C++ book example not compilling

    oki folks I got some doubts about he following code:
    Code:
    //Creating a vector that holds integers
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main(void){
    	vector<int> v;
    	for (int i = 0; i < 10; i++)
    		v.push_back(i);
    	for (int i = 0; i < v.size(); i++)
    		cout << v[i] << ", ";
    	cout << endl;
    	for (int i = 0; i < v.size(); i++)
    		v[i] = v[i] * 10;
    		cout << v[i] << ", ";
    	cout << endl;
    }
    When I compile it against g++ -Wall it reports the following errors:
    intvector.cpp: In function `int main()':
    intvector.cpp:10: warning: comparison between signed and unsigned integer
    expressions
    intvector.cpp:13: warning: comparison between signed and unsigned integer
    expressions
    intvector.cpp:15: error: name lookup of `i' changed for new ISO `for' scoping
    intvector.cpp:13: error: using obsolete binding at `i'

    So basically I got 2 questions:
    1 should I use my for variables as unsigned ints?
    2 why is the use of i to all loops invalid? is the book wrong?
    Thanks in advance.

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Is that code an exact copy of the code from the book? Because the indentation suggests that you need braces for a compound statement here:
    Code:
    for (int i = 0; i < v.size(); i++)
    	v[i] = v[i] * 10;
    	cout << v[i] << ", ";
    It should be
    Code:
    for (int i = 0; i < v.size(); i++)
    {
    	v[i] = v[i] * 10;
    	cout << v[i] << ", ";
    }
    Otherwise the cout statement is using i, but i doesn't exist in that scope because it was declared as a part of the for loop.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    1. v.size() returns an unsigned int (actually a size_t IIRC), so you should use an appropriate type to avoid that warning.

    2. The book is correct per the ISO standard - the real problem you have is that you don't have your for block (of more than one line) properly bracketed. You should have
    Code:
    for (size_t i = 0; i < v.size(); ++i) {
       v[i] = v[i] * 10;
       cout << v[i] << ", ";
    }
    Some folks (myself included) always put braces around the bodies of for/while loops, just so we don't forget and mess ourselves up like that.

    <edit>(I hate it when I post too slow!)</edit>

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Slacker
    Is that code an exact copy of the code from the book? Because the indentation suggests that you need braces for a compound statement here:
    Code:
    for (int i = 0; i < v.size(); i++)
    	v[i] = v[i] * 10;
    	cout << v[i] << ", ";
    It should be
    Code:
    for (int i = 0; i < v.size(); i++)
    {
    	v[i] = v[i] * 10;
    	cout << v[i] << ", ";
    }
    Otherwise the cout statement is using i, but i doesn't exist in that scope because it was declared as a part of the for loop.
    yes it is a perfect copy look:
    http://img523.imageshack.us/img523/617/tela12ql.jpg

    And should I declare the loop indexes as unsigned ints? Oh you suggestion fixed the code thanks lot.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    >> yes it is a perfect copy look:

    That image shows that it wasn't a perfect copy. That version has an extra for loop:
    Code:
    	for (int i = 0; i < v.size(); i++)
    		v[i] = v[i] * 10;
    	for (int i = 0; i < v.size(); i++)
    		cout << v[i] << ", ";

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Daved
    >> yes it is a perfect copy look:

    That image shows that it wasn't a perfect copy. That version has an extra for loop:
    Code:
    	for (int i = 0; i < v.size(); i++)
    		v[i] = v[i] * 10;
    	for (int i = 0; i < v.size(); i++)
    		cout << v[i] << ", ";
    Shame on me But again I re-ask the main doubt, should I declare the indexes of the loops as unsingned integers? why does g++ complains about it?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    The most correct way to declare the indexes would be to use vector<int>::size_type, because that is what size() returns. However, unsigned int would be fine if you just wanted to get rid of the warnings. Unless the size of the vector is greater than the maximum size a signed int can hold, using plain int wouldn't cause a problem. g++ is just warning you that it could be a problem if the numbers were big enough because the the index variable would never be able to hold the size.

    I use size_type, unsigned int, or size_t (which is also an unsigned value) depending on my mood.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Daved
    The most correct way to declare the indexes would be to use vector<int>::size_type, because that is what size() returns. However, unsigned int would be fine if you just wanted to get rid of the warnings. Unless the size of the vector is greater than the maximum size a signed int can hold, using plain int wouldn't cause a problem. g++ is just warning you that it could be a problem if the numbers were big enough because the the index variable would never be able to hold the size.

    I use size_type, unsigned int, or size_t (which is also an unsigned value) depending on my mood.
    Thx a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. URGENT: Help wanted...in C
    By iamjimjohn in forum C Programming
    Replies: 16
    Last Post: 05-18-2007, 05:46 AM
  2. Can't display book and borrower's details.
    By grscot in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2003, 10:18 AM
  3. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  4. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 07:03 PM

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