By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,652 Members | 1,842 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,652 IT Pros & Developers. It's quick & easy.

operator-overloading of nested class inside a template class

P: n/a
Hi,

I'm trying to implement a data-structure and have an iterator for it. Now I've
got a problem impleneting the operator+. I tried to isolate the problem:

template<int DIM, typename Tclass data_structure {
private:
int data[256];
public:
class iterator {
public:
int index;
};
};

template<int DIM, typename T>
data_structure<DIM, T>::iterator
operator+(data_structure<DIM, T>::iterator it, int n) {
iterator ret();
ret.index = it.index + n;
}

and g++ gives me this error:
bla.cpp:13: error: expected constructor, destructor, or type conversion before
'operator'

I just don't see, what I'm doing wrong here. Any hints would be appreciated.

Ciao,
Gerhard
Sep 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Gerhard Pfeiffer wrote:
Hi,

I'm trying to implement a data-structure and have an iterator for it.
Now I've got a problem impleneting the operator+. I tried to isolate
the problem:

template<int DIM, typename Tclass data_structure {
private:
int data[256];
public:
class iterator {
public:
int index;
};
};

template<int DIM, typename T>
data_structure<DIM, T>::iterator
operator+(data_structure<DIM, T>::iterator it, int n) {
iterator ret();
^^^^^^^^^^^^^^^^^^^^^^
This is a declaration of a function. Drop the parentheses.
ret.index = it.index + n;
}

and g++ gives me this error:
bla.cpp:13: error: expected constructor, destructor, or type
conversion before 'operator'

I just don't see, what I'm doing wrong here. Any hints would be
appreciated.
Read the FAQ. All of it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '06 #2

P: n/a
On 14 Sep 2006 18:04:59 GMT in comp.lang.c++, Gerhard Pfeiffer
<gp@bnbt.dewrote,
>template<int DIM, typename T>
data_structure<DIM, T>::iterator
operator+(data_structure<DIM, T>::iterator it, int n) {
iterator ret();
ret.index = it.index + n;
}
iterator is a type dependent on the template parameter; "typename"
is required.

template<int DIM, typename T>
typename data_structure<DIM, T>::iterator
operator+(typename data_structure<DIM, T>::iterator it, int n) {
iterator ret();
ret.index = it.index + n;
}
Sep 14 '06 #3

P: n/a
David Harmon wrote:
On 14 Sep 2006 18:04:59 GMT in comp.lang.c++, Gerhard Pfeiffer
<gp@bnbt.dewrote,
>template<int DIM, typename T>
data_structure<DIM, T>::iterator
operator+(data_structure<DIM, T>::iterator it, int n) {
iterator ret();
ret.index = it.index + n;
}

iterator is a type dependent on the template parameter; "typename"
is required.

template<int DIM, typename T>
typename data_structure<DIM, T>::iterator
operator+(typename data_structure<DIM, T>::iterator it, int n) {
iterator ret();
'ret' is still a function here...
ret.index = it.index + n;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.