![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
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); |
|
|
|
|
|
#3 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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;
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#4 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4
![]() |
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:.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#5 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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. ![]()
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#6 | |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
Quote:
|
|
|
|
|
|
|
#7 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 4
![]() |
>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.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#8 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
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). |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|