In C++ is there a way to define a class to be "comparable"? What Im trying to do is write a class that holds a string and a vector of numbers. If an object of this type is compared to another object I want the string that each object contains to be the "thing" thats compared. The header of my class so far is:

#ifndef index_h
#define index_h
#include <String>
using namespace std;
class  index{

 public:
  index(string word);
 void putpage(int number);
 bool haspage(int number);


};
#endif

Any input or help is appreciated, I'm new to c++

write an == operator

class  index{

 public:
  index(string word);
 void putpage(int number);
 bool haspage(int number);
 bool operator==(index& idx); // compare the two class objects

};

You can also write operator< , operator<= , etc.

Okay I have overloaded those operators and implemented it in my source file. Now my next question is what do I have to do to make this class able to be put into a Binary Search Tree. The BST I'm using does comparisons solely using the operator< so I think I have that set.

What type of syntax do I need to set my class to be of type comparable. Here is what my header file looks like after messing around and trying using template<type Comparable>. Note that I changed the class name to simply "structure"

#ifndef structure_h
#define structure_h
#include <vector>
#include <String>
#include "BinarySearchTree.h"
using namespace std;
template<class Comparable>
class BinarySearchTree;

 template<class Comparable> 
class structure{

public:
  string _word;
  vector<int> _indices;
  structure();
  structure(string word);
  structure(string word, int number);
 void putpage(int number);
 bool haspage(int number);
 bool operator< (const structure & rhs); // compare the two class objects

friend class BinarySearchTree<Comparable>;
};

#endif

Again, any input is greatly appreciated.

> What type of syntax do I need to set my class to be of type comparable?

nothing at all; just declare and implement the comparison operator(s). in

template<class Comparable>
class BinarySearchTree;

Comparable is just a place holder; the above is equivalent to

template< class T >
class BinarySearchTree;

and your structure could be

class structure
{
public:
  string _word;
  vector<int> _indices;
  structure();
  structure(string word);
  structure(string word, int number);
  void putpage(int number);
  bool haspage(int number);
  bool operator< (const structure & rhs) const ; // this should be a selector

  friend class BinarySearchTree<structure>;
};

this is how you could instantiate a BinarySearchTree of structures.

BinarySearchTree<structure> the_search_tree ;

and this would declare a function taking a binary search tree of structures as an argument

void function( BinarySearchTree<structure>& tree ) ;

Awesome thanks! Now I'm writing the implementation to the header file and I think the issues I'm getting are revolving around pointer issues, but I'm not sure. The pertinent code is:

structure::structure(){
    string _word;
    vector<int> *_indices();
}
//template <class Comparable>
structure::structure(string word){
   string _word;
   vector<int> *_indices();
    

}

//template <class Comparable>
structure::structure(string word, int number){
    string _word;
    vector<int> *_indices();
    
}

//template <class Comparable>
void structure::putpage(int number){
    *_indices->push_back(number);
}

//template <class Comparable>
bool structure::haspage(int number){
    vector<int>::iterator *iter;
    *iter=(_indices).begin();
    /*  for(*iter=*_indices->begin(); *iter!=*_indices->end();++*iter){
	if(*iter == number){
	    return true;
	}
	}*/
    return false;

What I'm trying to do with haspage(int number) is return a bool to see if the number is in the _indices vector. I'm also getting problems with putpage(int number) regarding void values not being ignored. Any help with syntax/pointer problems would be (again) appreciated.

> I think the issues I'm getting are revolving around pointer issues ...
there are several issues. let us take them one by one.

class structure
{

    public:
      string _word;
      vector<int> _indices;
      // ...
      structure() ; 

      // passing a string using const reference is more efficient;
      // we avoid the need to make a copy of a string
      structure( const string& word ) ;
      /// ...
      void putpage( int number ) ;
      bool haspage( int number ) ;
      // ....
};

these lines in the constructor(s) do nothing useful.

{
  // creates a string which is destroyed on return
  string _word;
 
  // this merely declares a function named _indices
  vector<int> *_indices();
}

both the string and the vector have default constructors which would initialize them correctly. so the default constructor needs to be just

structure::structure() 
{ /* default initialization for _word, _indices */ }

in a constructor, we could also initialize a member in a non-default way. eg.

structure::structure( const string& word ) : _word( word ) 
{ 
  // member _word initialized with word	 
  // default initialization for _indices 
}

to add a number to the vector indices

void structure::putpage( int number ) 
{ _indices.push_back( number ) ; }

to check weather a number is present in the vector indices, you could iterate through the vector looking for the number, or (easier) use the algorithm std::find

#include <algorithm>
bool structure::haspage( int number ) 
{ 
  return std::find( _indices.begin(), _indices.end(), number )
                != _indices.end() ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.