Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 26th, 2006, 7:31 PM   #1
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Jun 26th, 2006, 10:34 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 261
Rep Power: 4 Cache is on a distinguished road
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);
Cache is offline   Reply With Quote
Old Jun 27th, 2006, 8:44 AM   #3
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Jun 27th, 2006, 9:23 AM   #4
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 644
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Jun 27th, 2006, 9:42 AM   #5
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Jun 28th, 2006, 8:36 AM   #6
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
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.
Game_Ender is offline   Reply With Quote
Old Jun 28th, 2006, 9:17 AM   #7
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>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.
Narue is offline   Reply With Quote
Old Jun 28th, 2006, 9:26 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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).
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:55 PM.

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