Thread: iterator issues

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445

    iterator issues

    I have a function that returns the sum of elements in an std::vector, but I'm having issues with iterators. (line numbers added for clarity)

    Code:
    4 : template<class T> T sum(std::vector<T>& v)
    5 : {
    6 :   T tmp = 0;
    7 :   for (std::vector<T>::iterator it = v.begin(); it != v.end(); it++)
    8 :   {
    9 :     tmp += *it;
    10:   }
    11:   return tmp;
    12: }
    I'm compiling this on OpenSuse linux 10.3, kernel version 2.6.22.5-31-default, using GCC 4.2.1, and I get the following errors:

    xxx.cpp:7: error: expected ';' before 'it'
    xxx.cpp:7: error: 'it' was not declared in this scope

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    template<typename T> T sum(std::vector<T>& v)
    Try it with typename instead of class.
    "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
    Feb 2009
    Posts
    138
    vector<T>::iterator is a dependent type. you need the typename keyword to tell the compiler that it's a type and not a value.
    Code:
    4 : template<class T> T sum(std::vector<T>& v)
    5 : {
    6 :   T tmp = 0;
    7 :   for (typename std::vector<T>::iterator it = v.begin(); it != v.end(); it++)
    8 :   {
    9 :     tmp += *it;
    10:   }
    11:   return tmp;
    12: }

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by hk_mp5kpdw View Post
    Code:
    template<typename T> T sum(std::vector<T>& v)
    Try it with typename instead of class.
    typename and class are the same thing when used there.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Meldreth View Post
    vector<T>::iterator is a dependent type. you need the typename keyword to tell the compiler that it's a type and not a value.
    Code:
    4 : template<class T> T sum(std::vector<T>& v)
    5 : {
    6 :   T tmp = 0;
    7 :   for (typename std::vector<T>::iterator it = v.begin(); it != v.end(); it++)
    8 :   {
    9 :     tmp += *it;
    10:   }
    11:   return tmp;
    12: }
    that fixed it. thank you very much.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    oh yeah, and you can use the accumulate function in the numeric header to do this too.
    Code:
    #include <iostream>
    #include <numeric>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        cout << accumulate(v.begin(), v.end(), 0) << '\n';
    }

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Meldreth View Post
    typename and class are the same thing when used there.
    Not always. This is valid:

    Code:
    template <template <typename> class X>
    class foo {};
    Whereas this is not:

    Code:
    template <template <typename> typename X>
    class foo {};
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by brewbuck
    Not always.
    yes, always. without fail. even if you come up with a similar but not quite the same example that suggests otherwise. it's explained on this page if you still can't understand why your example doesn't prove me wrong.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Meldreth View Post
    yes, always. without fail. even if you come up with a similar but not quite the same example that suggests otherwise. it's explained on this page if you still can't understand why your example doesn't prove me wrong.
    What's your point?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by brewbuck
    What's your point?
    Meldreth's point is that typename is 'an alternative to "class" when declaring template arguments', so hk_mp5kpdw's suggestion was on the wrong track. I really don't see what is the fuss when "there" was not clearly specified, but the context is otherwise obvious.

    EDIT:
    Well, actually, Stroustrup's definition is inaccurate: "template arguments" should be changed to "template parameters".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Well, actually, Stroustrup's definition is inaccurate: "template arguments" should be changed to "template parameters".
    I would say "template type parameters". It seems easier to infer the intended meaning when choosing between "template type parameters", "template template parameters", and "template value parameters".

    Soma

Popular pages Recent additions subscribe to a feed