473,395 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Custom iterator for STL-style container

The container is:

template <typename T>
class Container {
public:
// container methods..

// iterator:
class const_iterator {
public:
const_iterator(T* i)
: pntr(i) { }

const_iterator& operator=
(const_iterator);
const_iterator& operator++();
const_iterator operator++(const_iterator);
const_iterator& operator--();
const_iterator operator--(const_iterator);
bool operator==(const_iterator);
bool operator!=(const_iterator);

private:
T* pntr;
};

const_iterator begin() const;
const_iterator end() const;

private:
std::vector<Tv;
};

// Well... implementation for Container<T>::begin() :
template <typename T>
Container<T>::const_iterator Container<T>::begin() const
{
const_iterator i(&v[0]);
return i;
}

g++ says: "error: expected constructor, destructor, or type conversion
before 'Container'"
Where is it wrong? Which is the right way to do that?

Thanks in advance,

s.

Mar 5 '07 #1
3 3278
"gallows" ,comp.lang.c++:
template <typename T>
Container<T>::const_iterator Container<T>::begin() const
Container<T>::const_iterator is a qualified (by Container<T>) and
dependent (on T) name; therefore, you have to prefix it by the keyword
"typename" to disambiguate the fact that it is the name of a type
(Container<T>::const_iterator could very well be the name of a data
member for some T).
Mar 5 '07 #2
On Mar 5, 10:49 am, "gallows" <g4ll...@gmail.comwrote:
The container is:

template <typename T>
class Container {
public:
// container methods..

// iterator:
class const_iterator {
public:
const_iterator(T* i)
: pntr(i) { }

const_iterator& operator=
(const_iterator);
const_iterator& operator++();
const_iterator operator++(const_iterator);
This should be:

const_iterator operator++(int);
const_iterator& operator--();
const_iterator operator--(const_iterator);
This should be:

const_iterator operator--(int);
bool operator==(const_iterator);
bool operator!=(const_iterator);

private:
T* pntr;
};

const_iterator begin() const;
const_iterator end() const;

private:
std::vector<Tv;

};

// Well... implementation for Container<T>::begin() :
template <typename T>
Container<T>::const_iterator Container<T>::begin() const
Add "typename" (see http://womble.decadentplace.org.uk/c...syntax-error):

typename Container<T>::const_iterator Container<T>::begin() const
{
const_iterator i(&v[0]);
return i;

}

g++ says: "error: expected constructor, destructor, or type conversion
before 'Container'"
Where is it wrong? Which is the right way to do that?
Cheers! --M

Mar 5 '07 #3
Clear, I Understood. Thanks a lot!

Mar 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
5
by: music4 | last post by:
Greetings, I want to STL map class. But I don't know how to use iterator. Let me state my question by following small sample: map<int, int> mymap; // insert some <key,value> in mymap ...
4
by: Chris Schadl | last post by:
Hi, I've written a simple sorted linked list class that I'm trying to implement an iterator for. I'm trying to make the interface to the iterator simmilar to the STL's iterator interface, so...
13
by: Grahamo | last post by:
Hi, I'm implementing a custom iterator (random access type ) so I can use stl algorithms such as sort on my legacy containers. I'm having problems compiling however. when implementing my...
8
by: Mateusz Ɓoskot | last post by:
Hi, I know iterator categories as presented by many authors: Stroustrup, Josuttis and Koenig&Moo: Input <---| |<--- Forward <--- Bidirectional <--- Random Output <---|
7
by: Allerdyce.John | last post by:
How can I emove an item in STL iterator without use the STL algorithm? I know we can use stl erase, find_if to remove items from a STL vector , but how can I do the same without using STL...
3
by: wolverine | last post by:
Hi I am accessing a map from inside threads. There is a chance that an element is inserted into the map, from inside any thread. Since i don't know about thread safety of stl implementation i am...
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
3
by: raan | last post by:
Is iterators singleton? I am accessing a map through wrapper functions to it; that allow me to access the contents of the map as CUnit &unit = hash.getFirstunit(); while(hash.hasMoreunits())...
11
by: Diego Martins | last post by:
for me, these items are in the 'tricky zone' of C++ does anyone know good material with that? (dealing with subtle details, pitfalls, good practices...) anything like the Effective series from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.