3

I had faced a circular dependency in my program and solved it with using pointers and forward declarations. I had made a file dn.hpp for this purpose. Everything was OK until I moved to shared pointers instead of generic. I created typedefs for shared pointers in dn.hpp. Everything works with generic pointers (commented out), bun not with shared.

It returns an error: field 'parentGraph' has incomplete type node.h line 21

(I am using mingw with boost 1.47)

Please help me to resolve this issue.

Here is simplified version of my code:

dn.hpp

// forward declarations
#include <map>
#include <list>
#include <boost/shared_ptr.hpp>

class graph;
typedef boost::weak_ptr<graph> graph_wp;
typedef boost::shared_ptr<graph> graph_sp;
//typedef graph* graph_wp;
//typedef graph* graph_sp;

class node;
typedef boost::weak_ptr<node> node_wp;
typedef boost::shared_ptr<node> node_sp;
//typedef node* node_wp;
//typedef node* node_sp;
typedef std::list<node_sp> nodes_t;

class edge;
typedef boost::weak_ptr<edge> edge_wp;
typedef boost::shared_ptr<edge> edge_sp;
//typedef edge* edge_wp;
//typedef edge* edge_sp;
typedef std::list<edge_sp> edges_t;
typedef std::list<edge_wp> edgeList_t;

graph.h

#ifndef GRAPH_H_
#define GRAPH_H_

#include "node.h"
#include "edge.h"

#include "dn.hpp"

class graph
{
public:
    node* createNode();
protected:
    nodes_t nodes;
    edges_t edges;
};
#endif /*GRAPH_H_ */

node.h

#ifndef NODE_H_
#define NODE_H_

#include "graph.h"
#include "edge.h"

#include "dn.hpp"

class node
{
public:
    node();
    node(graph_wp n);
    node(const graph_wp& net);
    virtual ~node();

    edge_wp createChild();
protected:
    graph_wp parentGraph;
    edgeList_t edges;
};
#endif /* NODE_H_ */

edge.h

#ifndef EDGE_H_
#define EDGE_H_

#include "node.h"

#include "dn.hpp"

class edge
{
public:
    edge(node_wp src,node_wp tgt);
    virtual ~edge();
private:
    node_sp source;
    node_sp target;
};
#endif /* EDGE_H_ */

main.cpp

#include "graph.h"
int main() {
    graph n;
    return 0;
}
Regexident
  • 29,441
  • 10
  • 93
  • 100
Yuriy Petrovskiy
  • 7,888
  • 10
  • 30
  • 34
  • Why do you say it is not working? – Stephen Nutt May 15 '12 at 19:28
  • @ Stephen Added error output. – Yuriy Petrovskiy May 15 '12 at 19:31
  • 1
    For whatever it is worth to you, your example was *very* long and hard to read. You'll get faster, more accurate answers if you reduce your program to the smallest complete program that demonstrates the error. Perhaps you'll even solve the problem yourself as you reduce it to a simple test case. See http://sscce.org/. Click [here](http://ideone.com/PXAsE) for an example of a short program that demonstrates the error you were having. – Robᵩ May 15 '12 at 19:39
  • Why does every header include every other? edge.h doesn't need to include node.h for instance. Surely the point of dn.hpp is to break those dependencies, why do you still (try to) include everything in every file? – Jonathan Wakely May 19 '12 at 23:22
  • @Jonathan Actually enge has node_wp members and it should include node.h. Eventually all of these file will be included. dh.hpp just allows to place forward declarations in the right way. And it it is example file after all. The real one is much more complex. – Yuriy Petrovskiy May 20 '12 at 07:06
  • But node_wp is defined in dn.hpp and so doesn't need node.h – Jonathan Wakely May 20 '12 at 07:22
  • @YuriyPetrovskiy: Quick reminder: ["In POSIX, names ending with _t are reserved, so if you are targeting a POSIX system (e.g., Linux), you should not end your types with _t."](http://stackoverflow.com/a/3225396/227536) – Regexident Mar 24 '13 at 13:33

1 Answers1

2

When I compile your code with VisualStudio I get

error C2079: 'node::parentGraph' uses undefined class 'boost::weak_ptr<Y>'
with
[
    Y=graph
]

Adding

#include <boost/weak_ptr.hpp>

fixes this

Stephen Nutt
  • 3,258
  • 1
  • 21
  • 21
  • Yes, that fixes it. Fought `weak_ptr` is part of `shared_ptr`. At least were were no errors with such messages with my compiler (gcc 4.7). Thank you. – Yuriy Petrovskiy May 15 '12 at 19:58