-1

I am almost new to boost-graph library. How to declare graph as a member variable?

typedef boost::adjacency_list<boost::vecS,
                          boost::vecS,
                          boost::undirectedS> Graph;
class myclass{
private:
    Graph graph;
void connect(const int N)
{
    // Graph graph(N); // make it local
    // or
    graph(N); //error
    for (size_t i = 0; i < N; i++)
    {
        for (size_t j = i + 1; j < N; j++)
        {
            if (adj[i][j] != 0)
                boost::add_edge(i, j, graph);
        }
    }
}

Edit: maybe we can make an instance of graph and pass it to the class

Graph graph(N);
myclass.set_params(graph);

Is it the only option?

Abolfazl
  • 1,047
  • 2
  • 12
  • 29
  • `graph(N);` --> `graph = Graph(N);`? – G.M. Oct 26 '19 at 13:54
  • You already have `graph` defined as a member variable. Your error is on the line where you try to manipulate the member variable. It's tough to tell what that line should be since you did not tell us what that line should do. – JaMiT Oct 26 '19 at 14:33
  • I should put an "or" in file after Graph graph(N); – Abolfazl Oct 26 '19 at 15:18

1 Answers1

1
graph(N); //error

That is not valid since graph is not callable; and you can't construct a member variable outside the constructor.

You will need to copy/move assign it, or use whatever member functions Graph has to set whatever properties you need.

I suggest picking up a good C++ book!

Acorn
  • 24,970
  • 5
  • 40
  • 69
  • I was looking for a valid way to construct it inside the class instead of building it outside and then pass it to the class. – Abolfazl Oct 26 '19 at 14:08
  • 1
    That's where the book comes in: http://coliru.stacked-crooked.com/a/2e97da379a591c64 (three ways to [re-]initialize) – sehe Oct 26 '19 at 20:01