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

  • Home
  • SEARCH
  • 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 6904127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:00:50+00:00 2026-05-27T08:00:50+00:00

I’m trying to implement a directed graph in C++. However, I’m having trouble with

  • 0

I’m trying to implement a directed graph in C++. However, I’m having trouble with my RemoveEdge function, after I call the function and it uses the delete operator on the pointer and set the pointer to nullptr, it is not nulling outside the scope of the function.

I’m not sure if I’ve stated my problem clearly enough, but maybe some code will help.

Graph.h

template<class TVertex, class TEdge, class TWeight>
class Graph
{
protected:
    std::list<Vertex<TVertex, TEdge, TWeight>*>* _Vertices;
    std::list<Edge<TVertex, TEdge, TWeight>*>* _Edges;
public:
    Graph();
    int TotalVertices();
    int TotalEdges();
    std::list<Vertex<TVertex, TEdge, TWeight>*>* Vertices();
    std::list<Edge<TVertex, TEdge, TWeight>*>* Edges();

    Vertex<TVertex, TEdge, TWeight>* FindVertex(const TVertex&);
    Vertex<TVertex, TEdge, TWeight>* InsertVertex(const TVertex&);
    void RemoveVertex(const TVertex&);

    Edge<TVertex, TEdge, TWeight>* FindEdge(const TEdge&);
    Edge<TVertex, TEdge, TWeight>* InsertEdge(const TVertex&, const TVertex&, const TEdge&, const TWeight&);
    void RemoveEdge(const TEdge&);
};

Graph.FindEdge()

template<class TVertex, class TEdge, class TWeight>
Edge<TVertex, TEdge, TWeight>* Graph<TVertex, TEdge, TWeight>::FindEdge(const TEdge& label)
{
    Edge<TVertex, TEdge, TWeight>* edge = nullptr;
    std::list<Edge<TVertex, TEdge, TWeight>*>::iterator it;

    for(it = this->_Edges->begin(); it != this->_Edges->end(); ++it)
    {
        if(label == (*it)->Label())
        {
            edge = *it;
            break;
        }
    }

    return edge;
}

Graph.RemoveEdge()

template<class TVertex, class TEdge, class TWeight>
void Graph<TVertex, TEdge, TWeight>::RemoveEdge(const TEdge& label)
{
    Edge<TVertex, TEdge, TWeight>* edge = this->FindEdge(label);
    if(edge == nullptr)
        return;

    this->_Edges->remove(edge);
    edge->Source()->RemoveEdge(edge);
    edge->Destination()->RemoveEdge(edge);

            // Problem is here, why isn't this working like I think it should?
    delete edge;
    edge = nullptr;
}

Main.cpp

// created graph
// added vertices
// added edges
Edge<string, string, int>* e5 = graph->InsertEdge("Oshawa", "Toronto", "E5", 5);
graph->RemoveEdge("E5");
cout << ((e5 == nullptr) ? "null" : "not null") << endl; // this outputs not null

So as you can see my program crashes after I remove the edge from the graph, for some reason it is outputting not null after the RemoveEdge function is executed. I’m not sure why this is happening, I’ve used the delete operator and I’ve also nulled the pointer explicitly after. What am I doing wrong here?

And yes, I’m sure the edge is being found, the FindEdge function finds the correct edge object and removes it from the appropriate lists but the delete operator is not doing what I want it to do.

Appreciate any help. Thanks in advance.

  • 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-27T08:00:50+00:00Added an answer on May 27, 2026 at 8:00 am

    To solve updating of e5 outside the function you can use shared_ptr and weak_ptr:

    template<class TVertex, class TEdge, class TWeight>
    class Graph
    {
      typedef Vertex<TVertex,TEdge,TWeight> VextexType;
      typedef Edge<TVertex,TEdge,TWeight> EdgeType;
    
      typedef shared_ptr<VertexType> VertexPtr;
      typedef shared_ptr<EdgeType> EdgePtr;
    
    protected:
        std::list< VertexPtr >* _Vertices;
        std::list< EdgePtr >* _Edges;
    public:
        Graph();
        int TotalVertices();
        int TotalEdges();
        std::list<VertexPtr>* Vertices();
        std::list<EdgePtr>* Edges();
    
        VertexPtr FindVertex(const TVertex&);
        VertexPtr InsertVertex(const TVertex&);
        void RemoveVertex(const TVertex&);
    
        EdgePtr FindEdge(const TEdge&)
        {
            for( std::list<EdgePtr>::iterator it = this->_Edges->begin(); it != this->_Edges->end(); ++it)
            {
                if( label == (*it)->Label())
                {
                    return *it;
                }
            }
            return EdgePtr();
        }
    
        EdgePtr InsertEdge(const TVertex&, const TVertex&, const TEdge&, const TWeight&);
        void RemoveEdge(const TEdge& label)
        {
            EdgePtr edge = this->FindEdge(label);
            if(!edge)
               return;
    
           this->_Edges->remove(edge);
           edge->Source()->RemoveEdge( edge.get() );
           edge->Destination()->RemoveEdge( edge.get() );
        }
    };
    

    Now you can write your section in main like this:

    weak_ptr<Edge<string, string, int> > e5( graph->InsertEdge("Oshawa", "Toronto", "E5", 5) );
    graph->RemoveEdge("E5");
    cout << (e5 ? "null" : "not null") << endl;
    

    Note that we use a weak_ptr to store the return value, rather than a shared_ptr.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.