Overloading Operator == C++
<p>The code for my question can be located at <a href="http://matthewh.me/Scripts/c++/graphs/" rel="nofollow">http://matthewh.me/Scripts/c++/graphs/</a>. 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.</p>

<p>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.</p>

<pre><code>template&lt;typename S,typename T&gt;
template&lt;typename U,typename V&gt;
bool Graph&lt;S,T&gt;::Node&lt;U,V&gt;::operator== ( Node&lt;S,T&gt; *comp ) {
    cout  &lt;&lt; "Searching for";
    comp-&gt;print_self( );
    bool ret = false;
    if( this-&gt;key == comp-&gt;key &amp;&amp; this-&gt;value == comp-&gt;value ) {
        ret = true;
    }
    return ret;
}
</code></pre>

<p>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.</p>

<p>First my driver:</p>

<pre><code>int main( int argc, char **argv ) {
    Graph&lt;int, int&gt; graph;
    pair&lt;int,int&gt; p1(3,4);
    pair&lt;int,int&gt; p2(3,4);

    graph.insert_adjacents( &amp;p1,&amp;p2 );

    graph.print_graph( );

    //graph.print_nodes( );
}
</code></pre>

<p>The insert function (There is a memory leak here not a concern atm):</p>

<pre><code>template&lt;typename S,typename T&gt;
void Graph&lt;S,T&gt;::insert_adjacents( pair&lt;S,T&gt; *from, pair&lt;S,T&gt; *to ) {
Node&lt;S,T&gt; *from_node = new Node&lt;S,T&gt;( from-&gt;first, from-&gt;second );
Node&lt;S,T&gt; *to_node = new Node&lt;S,T&gt;( to-&gt;first, to-&gt;second );

Node&lt;S,T&gt; *from_tmp = this-&gt;contains_already( from_node );
Node&lt;S,T&gt; *to_tmp = this-&gt;contains_already( to_node );

if( from_tmp==NULL ) {
    this-&gt;nodes.push_back( from_node );
    from_tmp = from_node;
}
if( to_tmp==NULL ) {
    this-&gt;nodes.push_back( to_node );
    to_tmp = to_node;
}

Node&lt;S,T&gt; *from_edge = from_tmp-&gt;contains_adjacent_node( to_tmp );
Node&lt;S,T&gt; *to_edge = to_tmp-&gt;contains_adjacent_node( from_tmp );

if( from_edge==NULL ) {
    from_tmp-&gt;insert_adjacent_node( to_node,rand( )%20 );
}
if( to_edge==NULL ) {
    to_tmp-&gt;insert_adjacent_node( from_node,rand( )%20 );
}
</code></pre>

<p>}</p>

<p>Now the contains functions for both Graph and Node are: </p>

<pre><code>template&lt;typename S,typename T&gt;
Graph&lt;S,T&gt;::Node&lt;S,T&gt;* Graph&lt;S,T&gt;::contains_already( Node&lt;S,T&gt; *check ) {
    cout &lt;&lt; "Searching for Node ";
    check-&gt;print_self( );
    cout &lt;&lt; " in node list" &lt;&lt; endl;
        typename list&lt;Node&lt;S,T&gt;* &gt;::iterator start = nodes.begin( );
        typename list&lt;Node&lt;S,T&gt;* &gt;::iterator end = nodes.end( );
        while( start != end ) {
                if( *(start) == check ) {
            cout &lt;&lt; "Found ";
            (*start)-&gt;print_self( );
            cout &lt;&lt; endl;
                        return (*start);
                }
                start++;
        }
        return NULL;
}
</code></pre>

<p>and</p>

<pre><code>template&lt;typename S,typename T&gt;
template&lt;typename U,typename V&gt;
Graph&lt;S,T&gt;::Node&lt;S,T&gt;* Graph&lt;S,T&gt;::Node&lt;U,V&gt;::contains_adjacent_node( Node&lt;S,T&gt; *adj ) {
    cout &lt;&lt; "Checking for adjacent node ";
    adj-&gt;print_self( );
    cout &lt;&lt; " next to ";
    this-&gt;print_self( );
    cout &lt;&lt; endl;
    typename list&lt;Edge*&gt;::iterator start = adj-&gt;adjacents.begin( );
    typename list&lt;Edge*&gt;::iterator end = adj-&gt;adjacents.end( );
    while( start != end ) {
        if( (*start)-&gt;adjacent == adj ) {
            return (*start)-&gt;adjacent;
        }
        start++;
    }
    return NULL;
}
</code></pre>

<p>Which both use the == operator to compare if two nodes are identical. Do I need to deference the pointers to make this work?</p>

<p>I know there are quite a few knowledgeable people out there, please help a younger C++ developer learn the ways.</p>

<p>Thank you,
Matthew Hoggan </p>

<hr>

<p>UPDATE</p>

<p>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.</p>

<pre><code>template&lt;typename S,typename T&gt;
template&lt;typename U,typename V&gt;
bool Graph&lt;S,T&gt;::Node&lt;U,V&gt;::operator== ( Node&lt;S,T&gt; &amp;comp ) const {
        cout  &lt;&lt; "Searching for";
        comp.print_self( );
        bool ret = false;
        if( this-&gt;key == comp.key &amp;&amp; this-&gt;value == comp.value ) {
                ret = true;
        }
        return ret;
}
</code></pre>

<p>and</p>

<pre><code>template&lt;typename S,typename T&gt;
template&lt;typename U,typename V&gt;
Graph&lt;S,T&gt;::Node&lt;S,T&gt;* Graph&lt;S,T&gt;::Node&lt;U,V&gt;::contains_adjacent_node( Node&lt;S,T&gt; *adj ) {
        cout &lt;&lt; "Checking for adjacent node ";
        adj-&gt;print_self( );
        cout &lt;&lt; " next to ";
        this-&gt;print_self( );
        cout &lt;&lt; endl;
        typename list&lt;Edge*&gt;::iterator start = adj-&gt;adjacents.begin( );
        typename list&lt;Edge*&gt;::iterator end = adj-&gt;adjacents.end( );
        while( start != end ) {
                if( (*(*start)-&gt;adjacent) == (*adj) ) {
                        return (*start)-&gt;adjacent;
                }
                start++;
        }
        return NULL;
}
</code></pre>

<p>and </p>

<pre><code>template&lt;typename S,typename T&gt;
Graph&lt;S,T&gt;::Node&lt;S,T&gt;* Graph&lt;S,T&gt;::contains_already( Node&lt;S,T&gt; *check ) {
        cout &lt;&lt; "Searching for Node ";
        check-&gt;print_self( );
        cout &lt;&lt; " in node list" &lt;&lt; endl;
        typename list&lt;Node&lt;S,T&gt;* &gt;::iterator start = nodes.begin( );
        typename list&lt;Node&lt;S,T&gt;* &gt;::iterator end = nodes.end( );
        while( start != end ) {
                if( (*(*(start))) == (*check) ) {
                        cout &lt;&lt; "Found ";
                        (*start)-&gt;print_self( );
                        cout &lt;&lt; endl;
                        return (*start);
                }
                start++;
        }
        return NULL;
}
</code></pre>
