0

I separated from a single file into a header and body files, but now have a problem.

I guess I am declaring something wrong but can't figure out what.

Body:

# include "GraphAL.h"


// A utility function to create a new adjacency list node
struct AdjListNode* GraphAL::newAdjListNode(int dest, int weight)
{
    struct AdjListNode* newNode =
            (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
    newNode->dest = dest;
    newNode->weight = weight;
    newNode->next = NULL;
    return newNode;
}

Header:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>


class GraphAL{
public:

    struct AdjListNode* newAdjListNode(int dest, int weight);


    // A structure to represent a node in adjacency list
    struct AdjListNode
    {
        int dest;
        int weight;
        struct AdjListNode* next;
    };

    // A structure to represent an adjacency liat
    struct AdjList
    {
        struct AdjListNode *head; // pointer to head node of list
    };

    // A structure to represent a graph. A graph is an array of adjacency lists.
    // Size of array will be V (number of vertices in graph)
    struct Graph
    {
        int V;
        struct AdjList* array;
    };
};

The error is: " cannot convert ‘GraphAL::AdjListNode*’ to ‘AdjListNode*’ in return "

3
  • struct AdjListNode* newAdjListNode(int dest, int weight); must come after the definition of struct AdjListNode. Also the use of malloc causes undefined behaviour, you could use new instead Commented Aug 29, 2019 at 23:30
  • lol indeed, thanks! I changed it to the end and at the body file I changed to "struct GraphAL::AdjListNode* GraphAL::newAdjListNode(int dest, int weight)". Also, thanks for the advice on malloc, you are right. Commented Aug 29, 2019 at 23:38
  • You don't need to put struct in cases where you refer to a variable in C++ like: struct AdjList* array, you can just put AdjList* array Commented Aug 30, 2019 at 3:39

1 Answer 1

2

You need to fully qualify return type in the implementation site:

GraphAL::AdjListNode* GraphAL::newAdjListNode(int dest, int weight)

In c++ use of malloc/free is discouraged for a couple of reasons; we tend to use new/delete instead:

AdjListNode* newNode =new AdjListNode;

There are a bunch of other things that can overally modernize and improve this snippet, but that I guess might cause confusion - if mentioned right now.

You may go ahead an do this:

auto GraphAL::newAdjListNode(int dest, int weight){
    //...
}

And:

auto newNode =new AdjListNode {dest, weight,nullptr};

Where nullptr is the proper keyword for null pointer value and auto is used for automatic type deduction.

I can go furthur and rewrite your code in a few short lines of declaration in header:

#include <forward_list>
#include <utility>

class GraphAL{
public: 
    typedef std::forward_list<std::pair<int,int>> Graph;
};

Then:

#include <GraphAL.h>
GraphAL::Graph gr1;
gr1.emplace_front(1,1);

Choice of std container(vector, list, ...) depends on detailed implementation.

Have fun.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.