0

I have a class called Point and an other namespace containing a tree class. this tree is a balanced binary search tree, and uses the function compare to compare its keys and puts them in the right place. My tree contains Points, and for each point I have to know which tree it belongs to, so I tried to add a pointer to the tree in my Point class like this:

class Point{
   public:
   double x, y;
   std::set<std::multiset<Point,Tree::compare()>*>s; //Tree is the name of the namespace
// some other data
}

My problem is that since my tree uses Point.h(because it stores Points) I cant add LayerThree.h to my it so I cant use Tree::compare() in my Point.h. I tried to add a new file like cmp.h and put my compare function in it, But It did not help. What should I do?

EDIT: I can not put both my tree and my Point class together in the same file because there are other files that needs to include Point.h

bido
  • 47
  • 6
  • 3
    Read how to [resolve circular dependencies in c++](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c?rq=1). – nwp Jul 27 '15 at 14:18
  • 4
    You need [forward declaration](https://en.wikipedia.org/wiki/Forward_declaration) – Jepessen Jul 27 '15 at 14:21
  • 1
    Why must `compare` be a member of `Tree`? And why must `Point` contain a weird set of things instead of just a `Tree*`? And could you edit the sentence *"My problem is..."*, it's very unclear and appears to contain too many pronouns and at least one editing error. – Beta Jul 27 '15 at 14:23
  • @beta it does not have to be a part of Tree. I tried and created a new file cmp.h and add my compare function to it, But again I faced the same problem. in Point.h I have to include cmp.h, and in cmp.h I need to include Point.h, because cmp compares two points, so again it fails – bido Jul 27 '15 at 14:27
  • You seem to be saying Tree is a namespace, not a class, so it should be easy to resolve the problem by forward declaring compare(). The more common and harder problem occurs when Tree is a class, so you can't forward declare compare() – JSF Jul 27 '15 at 14:27
  • 1
    In real projects, I would never have a mess like `std::multiset` in the middle of a bigger type specification. Your usage clearly implies that messy type must be specified in multiple places, which is very non maintainable. If it were not for your question, I would say that should all be wrapped in a typedef. But for your specific question (and most of the real situations similar to that, which arise in my own code) a struct inheriting from that works out better than a typedef (and gives you something you can forward declare). – JSF Jul 27 '15 at 14:33
  • http://stackoverflow.com/questions/31614278/fix-circular-dependency-in-arithmetic-class/31614518 – Alexander Balabin Jul 27 '15 at 14:35
  • I second @Jepessen: it sounds as if forward declaration will solve this problem quite easily. – Beta Jul 27 '15 at 14:40

1 Answers1

0

As Jepessen mentioned already you need forward declaration. Here is an example:

Class A depends on class B AND class B depends on class A, use forward declaration like this:

In Class "B.h":

#include "A.h"

class B
{
   //Do stuff
};

And in Class "A.h":

 // Forward declaration of class B, this tells the compiler to not worry about 
 // class B as somewhere later in compilation class B will be declared
class B;

class A
{
   //Do stuff
};
Griffin
  • 716
  • 7
  • 25