473,583 Members | 3,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning iterator from template class

Hi All,

I'm trying to write a wrapper class for std::vector to extend some of
its functionality. The problem I'm getting into is returning an
iterator type from a member function. Here is the essense of what I'm
trying to do:

//-------code-------

#include <vector>
using namespace std;

template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public:
vector<T>::iter ator begin()
{
return vals.begin();
}
};

//-------end code-------

However, this gives me the error

error: expected ';' before 'begin'

What's the proper way to achieve what I'm trying to do?

May 31 '06 #1
13 4553

jois.de.vi...@g mail.com wrote:

Implement thus:
template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public: // the following not only saves typing but allows you to
implement
// with a different object type other than vector...just
change the defs here
// and clients will fall in line so long as they don't try
to bypass your interface.

typedef typename vector<T>::iter ator iterator_t;
typedef typename vector<T>::cons t_iterator const_iterator_ t;
iterator_t begin() // changed return type to above typedefs
{
return vals.begin();
} // implement for const objects...
const_iterator_ t begin() const { return vals.begin(); } };

//-------end code-------


ExtendedVector< T> t;
ExtendedVector< T>::iterator_t it;

May 31 '06 #2

<jo***********@ gmail.com> wrote in message
news:11******** *************@g 10g2000cwb.goog legroups.com...
Hi All,

I'm trying to write a wrapper class for std::vector to extend some of
its functionality. The problem I'm getting into is returning an
iterator type from a member function. Here is the essense of what I'm
trying to do:

//-------code-------

#include <vector>
using namespace std;

template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public:
vector<T>::iter ator begin()

typename vector<T>::iter ator begin()

Regards,

Sumit.
--
Sumit Rajan <su*********@gm ail.com>
May 31 '06 #3
>
typedef typename vector<T>::iter ator iterator_t;
typedef typename vector<T>::cons t_iterator const_iterator_ t;
iterator_t begin() // changed return type to above typedefs
{
return vals.begin();
}

// implement for const objects...
const_iterator_ t begin() const { return vals.begin(); }


Wonderful! I guess my next question is, how did you know to do that? At
this point I don't even know what the 'typedef typename' part of the
above code snippet means. More specifically I suppose is, where can I
get a detailed discussion about templates. Any recommendations on
books? web sites?

May 31 '06 #4

jo***********@g mail.com wrote:
Hi All,

I'm trying to write a wrapper class for std::vector to extend some of
its functionality. The problem I'm getting into is returning an
iterator type from a member function. Here is the essense of what I'm
trying to do:

//-------code-------

#include <vector>
using namespace std;

template <class T>
class ExtendedVector
{
private:
vector<T> vals;
public:
vector<T>::iter ator begin()
{
return vals.begin();
}
};

//-------end code-------

However, this gives me the error

error: expected ';' before 'begin'

What's the proper way to achieve what I'm trying to do?


You need to point out that the vector<T>::iter ator is typename.
If you want to extend the vector, inheritance is better than
composition in your case.

May 31 '06 #5

<jo***********@ gmail.com> wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.com...
get a detailed discussion about templates. Any recommendations on
books? web sites?


Book Recommendation:
"C++ Templates" by David Vandevoorde, Nicolai M. Josuttis.

Regards,
Sumit.
--
Sumit Rajan <su*********@gm ail.com>
May 31 '06 #6
jo***********@g mail.com wrote:
Wonderful! I guess my next question is, how did you know to do that? At
this point I don't even know what the 'typedef typename' part of the
above code snippet means. More specifically I suppose is, where can I
get a detailed discussion about templates. Any recommendations on
books? web sites?


Basically, the "typename" keyword is used for what are called "dependant
names" in a template, which tells the compiler that what follows it is
the name of a type and not e.g. a static object of a class.

As always, the FAQ is a good starting point:
http://www.parashift.com/c++-faq-lite/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
May 31 '06 #7

jo***********@g mail.com wrote:

typedef typename vector<T>::iter ator iterator_t;
typedef typename vector<T>::cons t_iterator const_iterator_ t;
iterator_t begin() // changed return type to above typedefs
{
return vals.begin();
}

// implement for const objects...
const_iterator_ t begin() const { return vals.begin(); }


Wonderful! I guess my next question is, how did you know to do that? At
this point I don't even know what the 'typedef typename' part of the
above code snippet means. More specifically I suppose is, where can I
get a detailed discussion about templates. Any recommendations on
books? web sites?


Markus answered the question but it may have been a bit confusing.
Without the 'typename' keyword the compiler will think you are trying
to reference a value within the class:

class X
{
public: enum { v };
};

int val = X::v;

is more along the lines of what it thinks you mean because it can't
actually get complete information about the template at this point and
doesn't know what you are talking about. If it looks like a variable
it is unless it isn't....and right now it doesn't know that it isn't.

So you explicitly say, "The next name I use is a type name, not a
variable or value."

Really you are seing this:

typedef type new_name;

where type is "typename temp<type>::int ernal_type".

I did the typedef to abstract the internal representation of the class
so that if vector becomes the wrong container it can be changed without
affecting clients. They will have to be recompiled of course, but you
won't have to do a bunch of editing outside the class to reflect
changes done to its insides. You could even return your own iterator
that might be a simple pointer at some point. It isn't necissary to do
the typedefs, but it is good practice. All clients need to know is
that something that has the interface of an iterator will be returned
from those functions...the y don't need to know exactly what
implementation of an iterator they are getting (although it would be
important to know forward/reverse/random/insert...etc... more may be
necissary to truely encapsulate your type).

You can also read Bruce Eckel's Thinking in C++ V2 online and this
explains what typename is and where it needs to be used.

May 31 '06 #8

dan2online wrote:
You need to point out that the vector<T>::iter ator is typename.
If you want to extend the vector, inheritance is better than
composition in your case.


As a rule inheritance should be used only when necissary. It may well
be the case here but nothing in the OP indicates that.

May 31 '06 #9
dan2online wrote:
If you want to extend the vector, inheritance is better than
composition in your case.

Eehhhhh.

It's 'riskily dangerous' to inherit from vector<T> (or any other
standard containers) because they don't have virtual destructors. No
virtual destructors mean that if you ever end up storing your
super_vector<T> as a vector<T>*, and then delete that pointer, /the
universe explodes/. Or near enough.

There are few 'never's in programming, but as a rule of thumb, I have to
stop and think real hard about extending a class with no virtual destructor.
Jack Saalweachter
May 31 '06 #10

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

Similar topics

0
3391
by: sks_cpp | last post by:
I am trying to wrap the map iterator for keys and values. However, I seem to run into problems with the values for const_iterator - it works for all other combinations. Below I list my code and the compiler error. Please help if possible. Thanks in advance. // Compiler error ra:/home/ssangapu/personal/C++/STL/mapIter-738>g++ main.cc...
4
2497
by: Scott Smedley | last post by:
Hi all, I'm trying to write a special adaptor iterator for my program. I have *almost* succeeded, though it fails under some circumstances. See the for-loop in main(). Any pointers/help would be muchly appreciated. Apologies for the long post - I couldn't find a shorter way to
26
1514
by: Michael Klatt | last post by:
I am trying to write an iterator for a std::set that allows the iterator target to be modified. Here is some relvant code: template <class Set> // Set is an instance of std::set<> class Iterator { public : typedef typename Set::value_type T; typedef typename Set::iterator SetIterator; Iterator(Set& container, const SetIterator& it);
3
2025
by: Ken Cecka | last post by:
This is a contrived example to demonstrate a syntax problem I'm struggling with: #include <vector> template <typename T> class Class { };
13
2519
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} }; int main() { int arr = {1, 2, 3}; set<int,Asc> asc(arr, arr+3); set<int,Des>::iterator beg = asc.begin(); // ...
3
2394
by: chriscorbell | last post by:
I'm curious about what appears to be a restriction on using an STL container inside a user-defined template, esp. using an iterator to such a container. It's not clear to me if this is a general template/language restriction, and STL iterator limitation, or if I'm just going about it wrong. I'm declaring a template which uses a std::map to...
0
2666
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is...
3
2794
by: Belebele | last post by:
I have an Element class which is abstract and I would like to have an object of the Iterator class to iterate over a range of elements. I would like to use std::for_each to instrument the iteration, which forces me to have certain interface on the Iterator class. struct Element { virtual ~Element() = 0; };
3
1576
by: pnayak | last post by:
Hi, I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template (nList<T>::Link). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong? Many Thanks, Purush ...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7929
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6577
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5697
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3841
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1424
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1152
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.