Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6148841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:20:59+00:00 2026-05-23T19:20:59+00:00

The code for my question can be located at http://matthewh.me/Scripts/c++/graphs/ . The user name

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:21:00+00:00Added an answer on May 23, 2026 at 7:21 pm

    Of course you should dereference the pointers.

    In this code:

    (*start)->adjacent == adj
    

    (Assuming both adjacent and adj are 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:

    bool Graph<S,T>::Node<U,V>::operator== (const Node<S,T> &comp ) const
    

    Instead of what you have.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question I now have code that can attach to a
Simple question: Can I mix in my desktop application Java and JavaFX Script code?
Note : The code in this question is part of deSleeper if you want
Reading this question I found this as (note the quotation marks) code to solve
On this page: http://nerddinnerbook.s3.amazonaws.com/Part4.htm After the controller is added, I can browse to http://localhost:xxxx/dinners
After reading the answers to the question Calculate Code Metrics I installed the tool
Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update
the question I am having is: when running my app with a launch code
Some text before the code so that the question summary isn't mangled. class Tree
Note The question below was asked in 2008 about some code from 2003. As

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.