The code for my question can be located at http://matthewh.me/Scripts/c++/graphs/. The user name is guest password is guest. This will give you read access to the svn repository. I do this to save your eyes while reading this post.
Now to my question, which is with regards to overloading the == operator on a class. The code I have does not seem to be working but according to how I am interpreting it, it should (I need to be more humble and accept what the compiler says). Below is the portion of the code that is not working, as long as the chain of code segments that lead to it.
template<typename S,typename T>
template<typename U,typename V>
bool Graph<S,T>::Node<U,V>::operator== ( Node<S,T> *comp ) {
cout << "Searching for";
comp->print_self( );
bool ret = false;
if( this->key == comp->key && this->value == comp->value ) {
ret = true;
}
return ret;
}
This is used when building a graph to make sure I don’t insert duplicate Nodes. The Node and graph classes can be found in the code in the repository. To continue is the chain of blocks that lead to where this would be called.
First my driver:
int main( int argc, char **argv ) {
Graph<int, int> graph;
pair<int,int> p1(3,4);
pair<int,int> p2(3,4);
graph.insert_adjacents( &p1,&p2 );
graph.print_graph( );
//graph.print_nodes( );
}
The insert function (There is a memory leak here not a concern atm):
template<typename S,typename T>
void Graph<S,T>::insert_adjacents( pair<S,T> *from, pair<S,T> *to ) {
Node<S,T> *from_node = new Node<S,T>( from->first, from->second );
Node<S,T> *to_node = new Node<S,T>( to->first, to->second );
Node<S,T> *from_tmp = this->contains_already( from_node );
Node<S,T> *to_tmp = this->contains_already( to_node );
if( from_tmp==NULL ) {
this->nodes.push_back( from_node );
from_tmp = from_node;
}
if( to_tmp==NULL ) {
this->nodes.push_back( to_node );
to_tmp = to_node;
}
Node<S,T> *from_edge = from_tmp->contains_adjacent_node( to_tmp );
Node<S,T> *to_edge = to_tmp->contains_adjacent_node( from_tmp );
if( from_edge==NULL ) {
from_tmp->insert_adjacent_node( to_node,rand( )%20 );
}
if( to_edge==NULL ) {
to_tmp->insert_adjacent_node( from_node,rand( )%20 );
}
}
Now the contains functions for both Graph and Node are:
template<typename S,typename T>
Graph<S,T>::Node<S,T>* Graph<S,T>::contains_already( Node<S,T> *check ) {
cout << "Searching for Node ";
check->print_self( );
cout << " in node list" << endl;
typename list<Node<S,T>* >::iterator start = nodes.begin( );
typename list<Node<S,T>* >::iterator end = nodes.end( );
while( start != end ) {
if( *(start) == check ) {
cout << "Found ";
(*start)->print_self( );
cout << endl;
return (*start);
}
start++;
}
return NULL;
}
and
template<typename S,typename T>
template<typename U,typename V>
Graph<S,T>::Node<S,T>* Graph<S,T>::Node<U,V>::contains_adjacent_node( Node<S,T> *adj ) {
cout << "Checking for adjacent node ";
adj->print_self( );
cout << " next to ";
this->print_self( );
cout << endl;
typename list<Edge*>::iterator start = adj->adjacents.begin( );
typename list<Edge*>::iterator end = adj->adjacents.end( );
while( start != end ) {
if( (*start)->adjacent == adj ) {
return (*start)->adjacent;
}
start++;
}
return NULL;
}
Which both use the == operator to compare if two nodes are identical. Do I need to deference the pointers to make this work?
I know there are quite a few knowledgeable people out there, please help a younger C++ developer learn the ways.
Thank you,
Matthew Hoggan
UPDATE
I couldn’t resist and the wife is being a bit slow, so I decided to try and fix it before we left. Anyways, the two pieces of code updated are as follows, but they still don’t seem to be working. Any help now at this point is greatly appreciated.
template<typename S,typename T>
template<typename U,typename V>
bool Graph<S,T>::Node<U,V>::operator== ( Node<S,T> &comp ) const {
cout << "Searching for";
comp.print_self( );
bool ret = false;
if( this->key == comp.key && this->value == comp.value ) {
ret = true;
}
return ret;
}
and
template<typename S,typename T>
template<typename U,typename V>
Graph<S,T>::Node<S,T>* Graph<S,T>::Node<U,V>::contains_adjacent_node( Node<S,T> *adj ) {
cout << "Checking for adjacent node ";
adj->print_self( );
cout << " next to ";
this->print_self( );
cout << endl;
typename list<Edge*>::iterator start = adj->adjacents.begin( );
typename list<Edge*>::iterator end = adj->adjacents.end( );
while( start != end ) {
if( (*(*start)->adjacent) == (*adj) ) {
return (*start)->adjacent;
}
start++;
}
return NULL;
}
and
template<typename S,typename T>
Graph<S,T>::Node<S,T>* Graph<S,T>::contains_already( Node<S,T> *check ) {
cout << "Searching for Node ";
check->print_self( );
cout << " in node list" << endl;
typename list<Node<S,T>* >::iterator start = nodes.begin( );
typename list<Node<S,T>* >::iterator end = nodes.end( );
while( start != end ) {
if( (*(*(start))) == (*check) ) {
cout << "Found ";
(*start)->print_self( );
cout << endl;
return (*start);
}
start++;
}
return NULL;
}
Of course you should dereference the pointers.
In this code:
(Assuming both
adjacentandadjare pointers to your class objects) you’re comparing pointers, your operator is not invoked at all.BTW: I always thought you’re supposed to pass reference to the
operator==, i.e.: it should be:Instead of what you have.