Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   std::vector<T>::iterator not recognised? (http://www.programmingforums.org/showthread.php?t=10532)

Jessehk Jun 26th, 2006 8:31 PM

std::vector<T>::iterator not recognised?
 
I'm attempting to use std::find() to located a specific element in a std::vector, and for some reason, When I try to create a std::vector<T>::iterator, g++ complains.

Does anyone know my problem?

:

#include <vector>
#include <iostream>
#include <cstddef>
#include <stdexcept>
#include <string>
#include <iterator>
#include <algorithm>

template<typename T, size_t capacity=5>
class Basket {
    private:
        const size_t m_capacity;
        std::vector<T> m_items;
    public:
        class CapacityError : public std::length_error {
            public:
                CapacityError(const std::string &msg)
                    : length_error(msg) {
                }
        };

        class ItemNotFoundError : public std::invalid_argument {
            public:
                ItemNotFoundError(const std::string &msg)
                    : invalid_argument(msg) {
                }
        };
       
        Basket()
            : m_capacity(capacity),
              m_items(0) {
        }

        void add_item(const T &item) {
            if(m_items.size() + 1 > m_capacity)
                throw CapacityError("The Basket it full!");
            else
                m_items.push_back(item);
        }

        void remove_item(const T &item) {
            std::vector<T>::iterator pos;
            pos = find(m_items.begin(), m_items.end(), item);


            if(pos != m_items.end())
                m_items.erase(pos);
            else
                throw ItemNotFoundError("The specified item was not found in the Basket.");
        }
};

int main() {
    Basket<int> basket;

    return 0;
}


ERRORS
:

basket.cpp: In member function ‘void Basket<T, capacity>::remove_item(const T&)’:
basket.cpp:42: error: expected `;' before ‘pos’
basket.cpp:43: error: ‘pos’ was not declared in this scope


Cache Jun 26th, 2006 11:34 PM

Compiles fine in VC++ 2005, but not Dev-C++ (with g++.exe). It works OK if you typename it thought.

:

typename std::vector<T>::iterator iter =
  std::find(m_items.begin(), m_items.end(), item);


Narue Jun 27th, 2006 9:44 AM

>std::vector<T>::iterator pos;
vector<T>::iterator is a type that depends on a template argument (T being that argument). In such a case you have to specify that you're referring to a type rather than a "thing" by using the typename keyword:
:

typename std::vector<T>::iterator pos;

Jessehk Jun 27th, 2006 10:23 AM

Thanks Narue and Cache. :)

There is so much more to C++ then I once thought. I know the basics, but some template and class features are incredibly complicated :eek:.

Narue Jun 27th, 2006 10:42 AM

>some template and class features are incredibly complicated
I think that templates are the most complicated feature of C++. It's basically a turing complete sub-language. There's a lot of power, but only if you can wrap your brain around it. :)

Game_Ender Jun 28th, 2006 9:36 AM

Quote:

Originally Posted by Cache
Compiles fine in VC++ 2005, but not Dev-C++ (with g++.exe). It works OK if you typename it thought.

On a side not, is it standards compliant to compile without the typename? I have done some pretty heavy template stuff with GCC 4.0.x and it always requires typename, the error messages even tell you to use it. I was quite supprised to see a helpful error message from the compilier.

Narue Jun 28th, 2006 10:17 AM

>On a side not, is it standards compliant to compile without the typename?
No, in strict mode, a compliant compiler should produce a diagnostic message.

grumpy Jun 28th, 2006 10:26 AM

Quote:

Originally Posted by Narue
No, in strict mode, a compliant compiler should produce a diagnostic message.

You're right but, at the risk of being picky .... I would probably make the statement outside in, whereas you have made it inside out.

A compliant compiler should produce a diagnostic. A compiler that is non-compliant may not. Some compilers, by default, are a bit relaxed about standard compliance (i.e. the "strict mode", as you describe it, is disabled by default).


All times are GMT -5. The time now is 8:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC