c++ - when defining a private structure in a class how do i use it as a function parameter or return type? -


i have created graph.h, graph.cpp, , main.cpp in graphics.h i've created 2 private structures edge , node, , i've declared edge first above node edge has node member i've forward declared node struct i'm running issues saying "graph::node*" incompatible "node *" occurs in graph.cpp addedgebyname(string,string,double) function, why happen? on top of dont know why had declare in (graphics.cpp) function graph::node * graph::findbyname(string name) thought way tried declare node * graph::findbyname(string name) work gave me error. im trying make directed weighted graph way.

#include <iostream> #include <string> #include <vector>    using std::cout; using std::cin; using std::endl; using std::string; using std::vector;  struct node;  class graph{      private:         typedef struct edge{             double edgeweight;             node *pointsto;              edge(){}             edge(const edge & copyobject){                 this->edgeweight = copyobject.edgeweight;                 this->pointsto = copyobject.pointsto;             }          } edge;          typedef struct node{             string name;             vector<edge> myedges;// list of outgoing edges node              node(string myname){                 name = myname;             }              node(const node & copyobject){                 this->name = copyobject.name;                 this->myedges = copyobject.myedges;             }             // adds edge node             void addedge(node *pointto, double weight){                 edge newedge;                 newedge.edgeweight = weight;                 newedge.pointsto =                  newedge.pointsto = pointto;                 myedges.push_back(newedge);             }         } node;          vector<node*> graph; // vector containing nodes                              //graph     public:         graph();         // finds node in list of nodes graph, using name         node * findbyname(string name);          void add(string name);          void addedgebyname(string first, string second,double weight);   };  #endif     // graphics.cpp  #include "graph.h"   graph::graph(){    }  graph::node * graph::findbyname(string name){        for(std::vector<node*>::iterator = graph.begin(); != graph.end(); ++it){          if((*it)->name == name){             return *it;         }     }     return null; }  void graph::add(string name){      node * newnode = new node(name); // creates new node given                 //name     graph.push_back(newnode); // adds node our list of nodes in //graph }  // publicly accessible function adds edges each of strings  void graph::addedgebyname(string first, string second,double weight){      node * firstnode = findbyname(first);     node * secondnode = findbyname(second);     firstnode->addedge(secondnode,weight);     secondnode->addedge(firstnode,weight);  } 

don't use typedef struct node { … } node in c++. that's c.

node structure inside graph need forward-declare there, not outside it:

class graph { private:     struct node;      struct edge {         …     };      struct node {         …     }; }; 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -