-3

i trying to take some code from a computation geometry book and i found this error need help!

class Point2D{
        ..
        ..
        some functions
        ..
        ..

        int classify(Point2D &p0, Point2D &p1) {
                Point2D p2 = *this;
                Point2D a = p1 - p0;
                Point2D b = p2 - p0;
                double sa = a.x * b.y - b.x * a.y;

                if (sa > 0.0) return LEFT;
                if (sa < 0.0) return RIGHT;
                if ((a.x * b.x < 0.0) || (a.y * b.y < 0.0)) return BEHIND;
                if (a.length() < b.length()) return BEYOHD;
                if (p0 == p2) return ORIGIN;
                if (p1 == p2) return DESTINATION;
                return BETWEEN;
        }

        int classify(Edge2D ed) {
                return Point2D::classify(ed.org, ed.dest);
        }


};

class Edge2D {
public:
        Point2D org;
        Point2D dest;

        ...bla bla bla

};

compiler errors:

syntax error: identifier 'Edge2D'
'ed': undeclared identifier
left of '.org' must have class/struct/union
left of 'dest' must have class/struct union
'Point2D::classify': no overloaded function takes 1 arguments

help ! :(

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    Class `Edge2D` must be seen before declaration of `Point2D`. How to solve this you can [read here](http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c). – πάντα ῥεῖ Apr 03 '16 at 08:55

1 Answers1

0

Use forward declaration of Edge2D class before Point2D class as:

class Edge2D;
class Point2D{
    // your class members...
};
class Edge2D{
    // your class members..
};
Shiv
  • 122
  • 2
  • 16
  • if you implement both classes in the same file it will not work. the compiler will show error : using undefined type of ...... but it works for me when i implemented -- the method of first class that uses the second class -- after the second class something like this class Edge2D; class Point2D{ int classify(Edge2D &e); // declared here but must be defined after class edge2D class } class edge2D{ Point2D p1, p1; } int Point2D::classify(Edge2D &e){ lab lab la; } this gonna work ) – Mohamed Khaled Apr 05 '16 at 23:18
  • @MohamedKhaled my answer will also work. Did you checked my answer? – Shiv Apr 06 '16 at 05:56