operator-overloading of nested class inside a template class

G

Gerhard Pfeiffer

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 T> class 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
 
V

Victor Bazarov

Gerhard said:
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 T> class 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
 
D

David Harmon

On 14 Sep 2006 18:04:59 GMT in comp.lang.c++, Gerhard Pfeiffer
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;
}
 
V

Victor Bazarov

David said:
On 14 Sep 2006 18:04:59 GMT in comp.lang.c++, Gerhard Pfeiffer


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top