Dijkstra Program

I won’t go through each error exhaustively, but here is a general strategy for resolving these errors.

When dealing with error messages from the compiler, it is generally a good practice to start with the first error, deal with it, and then attempt to compile again. Oftentimes, a single issue will result in multiple error messages, and resolving one problem can give clues to the rest. If nothing else, handling one thing at a times helps keep things manageable.

Let’s look at that first error message:

dijkstra.cpp: In function ‘void insertEdge(int, int, int, Graph*)
dijkstra.cpp:152:10: error: no matching function for call to ‘Edge::Edge()’
    Edge newEdge;
         ^

1. Look at the line that is triggering the error.

Sometimes there will be something obvious at that line (or perhaps the line above it). A missing semi-colon, a forgotten argument, a typo, etc.

Your compiler has told you where to look. The error is in dijkstra.cpp at line number 152. It’s also given us the signature of the function it is in: void insertEdge(int, int, int, Graph*). In fact, it’s even pinpointed where it realized there was something wrong: when your program tries to create an Edge called newEdge.

2. Interpret the error message.

For non-obvious mistakes, you may have to dig into what the error message is saying. In this case the error listed is: no matching function for call to 'Edge::Edge()'.

That seems fairly self-explanatory to me, but of course, that depends upon the experience level of the person reading the message. If I don’t understand an error message, I employ a simple strategy: I Google it. Removing the names specific to your program will be useful, so in this case I would likely search for "error: no matching function for call to". Doing so brings up many results for programmers facing similar errors.

In this specific case, the compiler is saying that you are trying to call the function Edge::Edge() but it can’t find a function that matches that. Edge::Edge() would be the constructor for your Edge struct – but you’ve only defined one constructor, and it takes several arguments, so the compiler doesn’t know how to construct an Edge without any arguments.

3. Fix the error

For this specific error, you could change the referenced line to create an Edge using the arguments. Something like:

Edge newEdge(0, 0, 0.0, nullptr)

But that’s probably not what you want. More likely, you want to add a default constructor to your Edge struct.

struct Edge 
{
    ...
    Edge(int u, int v, double w, Edge* nextEdge)
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
    Edge() = default; // Creates a default constructor; leaves values uninitialized
};

or

struct Edge 
{
    ...
    Edge(int u, int v, double w, Edge* nextEdge)
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
    Edge() : from(0), to(0), weight(0.0), next(nullptr) {} // Initializes members to a set of defaults
};

or

struct Edge 
{
    ...
    Edge(int u = 0, int v = 0, double w = 0.0, Edge* nextEdge = nullptr) // Defines default values so that constructor can be called with 0-4 arguments
    {
        from = u;
        to = v;         
        weight = w;     
        next = nextEdge;
    }
};

4. Repeat

Now you re-compile your program. Hopefully, the number of error messages has decreased. Regardless, you look at the first error on the list, and you repeat this process until your program compiles without errors.

Leave a Comment