Hello,
I wrote a STL-like container, which represents a binary tree and which includes iterators. I overloaded the operator '--' in a particular way. Suppose that 'It' is an iterator, then '--It' will move the iterator to the left son, and 'It--' will move the iterator to the right son. And '++It' and 'It++' are equivalent and move the iterator to the father. So far so good, the code compiles and it does what I want.
The problem comes when I want to overload the operator '-'. I would like to be able to write something like:
1 2
|
NewIt=1-It; // NewIt should point on the left son of It
NewIt=It-1; // NewIt should point on the right son of It.
|
So here is the (simplified) code that I wrote:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
class ClassA
{
int MyData;
// ...
};
template <class Type>
class ClassB
{
public: class Iterator;
private:
class Node
{
private:
Type Data;
public:
friend class ClassB::Iterator;
};
Node *RootNode;
public:
class Iterator
{
Node *It;
public:
Iterator(void){};
Iterator(Node *R) {It=R;}
};
};
template <class Type> const typename ClassB<Type>::Iterator operator-(int n,const typename ClassB<Type>::Iterator &It)
{
typename ClassB<Type>::Iterator New=It;
// ...
return New;
}
int main(void)
{
ClassB<ClassA>::Iterator It1,It2;
// It2=1-It1; // Code doesn't compile if this line is uncommented
return 0;
}
|
If I uncomment the line 'It2=1-It1' in the main function, then the code doesn't compile and gives me the following error:
"In function ‘int main()’:
Test.cpp:50:11: error: no match for ‘operator-’ in ‘1 - It1’
Test.cpp:50:11: note: candidate is:
Test.cpp:40:61: note: template<class Type> const typename ClassB::Iterator operator-(int, const typename ClassB<Type>::Iterator&)"
Note that the code will compile and do I want if I write instead
|
It2=operator-<ClassA>(1,It1);
|
Can anyone tell me what is wrong? Note that, if I don't define ClassB as a template, but write it explicitly with the type ClassA, then the code compiles with no problem. So it seems that the mistake is related to the fact that I use a template parameter.
Thanks for your help!