Hey wassup, it been a while, but lately I've been busy with school.
I've been working on code for C++. What I'm basically trying to do is make an array of trees objects.
So I have a class A and a class B and a class C. All three classes are templated.
A has one member of type B< C<T> >*. So it looks like ...
template < class T >
class A {
public:
B< C<T> >* m_member;
}
This explains very simply what I'm trying to do, that is not the problem.
Now more specfic. My B class is a BinarySearch Tree and my C class is a Key class containing a string and a T member. Now when I try to create an object of BSTree< Key<T> > or BSTree< Key<int> > (or whatever) it gives me an error. Basically the Tree class in many of its functions has to compare two T types, specifically uses the operator> and operator< but Key<T> doesn't have those operators overloaded, so my complier got mad at me.
So I went into the Key class and defined both operators like this...
template < class T >
bool Key<T>::operator<(Key<T> &k) {}
template < class T >
bool Key<T>::operator>(Key<T> &k) {}
both are member functions (as opposed to friend functions)
But my complier still mad at me it says...
MyBSTree.hpp:156:3: error: no match for ‘operator>’ in ‘x > p->TreeNode<Key<int> >::m_data
without pasting the whole code here is what the function looks like that it is getting this error
Code:
I don't think its my tree class, it work's fine with int, floats, whatever (famous last words) And I even tested the Key classes compare with two Key objects and it works, so I am not seeing why its not working there.
So I guess my problem is how to you define overloaded operator< and operator> for templated classes?
As friend functions? member functions? One or two parameters? Some sort of extra argurment?
I hoped thats not too simplied, but I just wanted to cover a lot of bases just so if I can't immediately reply.
I've been working on code for C++. What I'm basically trying to do is make an array of trees objects.
So I have a class A and a class B and a class C. All three classes are templated.
A has one member of type B< C<T> >*. So it looks like ...
template < class T >
class A {
public:
B< C<T> >* m_member;
}
This explains very simply what I'm trying to do, that is not the problem.
Now more specfic. My B class is a BinarySearch Tree and my C class is a Key class containing a string and a T member. Now when I try to create an object of BSTree< Key<T> > or BSTree< Key<int> > (or whatever) it gives me an error. Basically the Tree class in many of its functions has to compare two T types, specifically uses the operator> and operator< but Key<T> doesn't have those operators overloaded, so my complier got mad at me.
So I went into the Key class and defined both operators like this...
template < class T >
bool Key<T>::operator<(Key<T> &k) {}
template < class T >
bool Key<T>::operator>(Key<T> &k) {}
both are member functions (as opposed to friend functions)
But my complier still mad at me it says...
MyBSTree.hpp:156:3: error: no match for ‘operator>’ in ‘x > p->TreeNode<Key<int> >::m_data
without pasting the whole code here is what the function looks like that it is getting this error
Code:
template < class T >
int MyBSTree<T>::contains(TreeNode<T>* p,const T& x,int &counter)const
{
if (p == NULL)
{
return -counter;
}
if (x > ( (*p).m_data) )
{
counter++;
return contains(p->m_right,x,counter);
}
if (x < ( (*p).m_data) )
{
counter++;
return contains(p->m_left,x,counter);
}
return counter;
}
I don't think its my tree class, it work's fine with int, floats, whatever (famous last words) And I even tested the Key classes compare with two Key objects and it works, so I am not seeing why its not working there.
So I guess my problem is how to you define overloaded operator< and operator> for templated classes?
As friend functions? member functions? One or two parameters? Some sort of extra argurment?
I hoped thats not too simplied, but I just wanted to cover a lot of bases just so if I can't immediately reply.