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:

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.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement