Question

I'm writing a C++ program that, unfortunately, requires the use of a C++98 compiler. After writing the program and trying to compile I get this error:

error: invalid use of non-static data member 'graph::adj_list'

This is confusing to me since I used cppreference (http://en.cppreference.com/w/cpp/language/data_members) to make sure this would compile. Can anybody help me out?

graph_frame.h

#include <cstddef>


class node{
friend class graph;
private:
    int node_name;
    int edge_weight;
    node *next_cell = NULL;
    node *previous_cell = NULL;

public:
    node(int name = 0, int new_edge = 0);
    ~node();
};


class graph{
private:
    void recursive_delete(int i, int k);
    node** adj_list;                     <----------------ERROR

public:
    //----argument not needed for constructor because a list is not created
    //----until create_graph() is called
    graph();
    ~graph();
    //----remember to free all buckets from list
    void create_graph(int& graph_nodes);
    int check_node(int& source_node, int& dest_node);
    void insert_node(int& source_node, int& dest_node, int& weight);
    void print(node **dummy = adj_list, int i = 0, int k = 0);
    void delete_node(int& source_node, int& dest_node);
};
Was it helpful?

Solution

node *next_cell = NULL;
node *previous_cell = NULL;

That won't fly - in-class initializers for non-static members were only introduced in C++11.

About adj_list, the real location of the error is here:

void print(node **dummy = adj_list, int i = 0, int k = 0);
//                      ^^^^^^^^^^

You can't have a class member as a default value because it's not known to which instance it should belong. You can't use this there either, so you'll have to figure out some other way to pass a default value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top