c++ - How to declare two structs which have members of the others type? -


i trying make directed graph, made graph class , has private edge struct , private node struct. edges have node member node edge points to, , nodes have list of edges lead away them.

#ifndef directed_graph_h  #define directed_graph_h  #include <iostream> #include <vector> #include <string>  class graph { public:     graph( const std::string & );     ~graph();      void print();  private:     struct gnode     {         std::string currency_type;         std::vector<gedge> edges; // line 19          gnode( std::string name ) : currency_type( name ) {}     };      struct gedge     {         int weight;         gnode * node; // node edge pointed towards          gedge( int weight, gnode* node ) : weight( weight ), node( node ) {}     };      gnode *source;     std::vector<gnode> nodes;       void add_node( const std::string & currency );     void add_edge( const gnode *& source, const gnode *& destination, int weight );     std::string bellman_ford( const gnode *&source ); };  #include "directed_graph.cpp"  #endif 

the problem is, first struct being declared, in case gnode, not aware gedge exists causing compiler give me error

directed_graph.h:19: error: iso c++ forbids declaration of ‘vector’ no type 

how can around this?

just use forward declaration:

class graph {      // ...  private:      struct gedge; //  ^^^^^^^^^^^^^ //  forward declaration gedge      struct gnode     {         std::string currency_type;          std::vector<gedge> edges; // <== ok because of                                   //     forward declaration above          gnode( std::string name ) : currency_type( name ) {}     };      struct gedge // <== comes definition of gedge     {         int weight;         gnode * node; // node edge pointed towards          gedge( int weight, gnode* node )              : weight( weight ), node( node ) {}     };      // ... }; 

here complete live example of above code compiling.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -