double include

I have classes Shape and Document.
Document holds Shapes.

Document has a list<Shape*> _shapes.
But I would like to have a pointer to Document in Shape, too.
And I would like to call some Document methods from Shape.

Here is short version of code:

document.h
1
2
3
4
5
6
include "shape.h"
class Document
{
  list<Shape*> _shapes;
  //...
}


shape.h
1
2
3
4
5
6
class Document;
class Shape
{
  document* _pointerToMyDocument;
  //...
}


Now when I call _pointerToMyDocument->SetSomething();
compilation fail with error:
1
2
invalid use of undefined type `struct Document'
forward declaration of `struct Document'


Do I nead to put #include "document.h" in Shape.

Is it ok to do next ??

document.h
1
2
include "shape.h"
class Document {}


shape.h
1
2
include "document.h"
class Shape {}


Because now there is double relationship...
The solution is to put the forward declaration in the header files and the include in the source files.
Last edited on
Is it a typo?

1
2
3
4
5
6
class Document;
class Shape
{
  document* _pointerToMyDocument;
  //...
}


Now when I call _pointerToMyDocument->SetSomething();
compilation fail with error:

invalid use of undefined type `struct Document'
forward declaration of `struct Document'


I think that the code where you are using the call _pointerToMyDocument->SetSomething(); does not see yet the definition of class Document. Maybe you should place the code where you are using this call after the definition of class Document.
Thank You for your comments..

I did it the way Peter87 says..
Topic archived. No new replies allowed.