![]() |
| | #1 |
| Registered User Join Date: Jul 2005
Posts: 41
| Header files and classes I have a couple of classes defined, one class and structures defined that are from another class. So I have for example: Code: class Edge {
public:
Point org;
Point dest;
Edge(Point &_org, Point &_dest);
Edge(void);
Edge &rot(void);
Edge &flip(void);
Point point(double);
int intersect(Edge&, double&);
int cross(Edge&, double&);
bool isVertical(void);
double slope(void);
double y(double);
};
Code: class Point{
public:
double x;
double y;
Point(double _x = 0.0, double _y = 0.0);
Point operator+(Point&);
Point operator-(Point&);
friend Point operator*(double, Point&);
double operator[](int);
int operator==(Point&);
int operator!=(Point&);
int operator<(Point&);
int operator>(Point&);
int classify(Point&, Point&);
int classify(Edge&);
double polarAngle(void);
double length(void);
double distance(Edge&);
};
Code: all: Poly Poly: Point.o Edge.o Polygons.o g++ -O4 -o Poly Point.o Edge.o Polygons.o Point.o: Point.cpp g++ -O4 -c -Wno-deprecated -Wall Point.cpp -o Point.o Edge.o: Edge.cpp g++ -O4 -c -Wno-deprecated -Wall Edge.cpp -o Edge.o Polygons.o: Polygons.cpp g++ -O4 -c -Wno-deprecated -Wall Polygons.cpp -o Polygons.o clean: find . -name '*.o' | xargs rm -f ; Edge.h:8: error: ‘Point’ does not name a type Edge.h:9: error: ‘Point’ does not name a type Edge.h:10: error: expected `)' before ‘&’ token Edge.h:14: error: ‘Point’ does not name a type BTW, a typical .h function looks like this: Code: #ifndef _Edge_h
#define _Edge_h
#include "Point.h"
class Edge {
public:
Point org;
Point dest;
Edge(Point &_org, Point &_dest);
Edge(void);
Edge &rot(void);
Edge &flip(void);
Point point(double);
int intersect(Edge&, double&);
int cross(Edge&, double&);
bool isVertical(void);
double slope(void);
double y(double);
};
// Edge::Edge(Point &_org, Point &_dest) :
// org(_org), dest(_dest)
// {
//
// }
//
// Edge::Edge(void) :
// org(Point(0,0)), org(Point(1,0))
// {
//
// }
//
// Edge &Edge::rot(void)
// {
// Point m = 0.5 * (org + dest);
// Point v = dest - org;
// Point n(n.y, -v.x);
// org = m - 0.5 *n;
// dest = m + 0.5 *n;
// return *this;
// }
//
// Edge &Edge::flip(void)
// {
// return rot().rot();
// }
//
// Point Edge::point(double t)
// {
// return Point(org + t * (dest - org));
// }
//
// enum { COLLINEAR, PARALLEL, SKEW, SKEW_CROSS, SKEW_NO_CROSS};
//
// double dotProduct(Point &p, Point &q)
// {
// return (p.x * q.x + p.y *q.y);
// }
//
// int Edge::intersect(Edge &e, double &t)
// {
// Point a = org;
// Point b = dest;
// Point c = e.org;
// Point d = e.dest;
// Point n = Point((d-c).y, (c-d).x);
// double denom = dotProduct(n, b-a);
// if (denom == 0.0)
// {
// int aclass = org.classify(e);
// if ((aclass==LEFT) || (aclass==RIGHT))
// return PARALLEL;
// else
// return COLLINEAR;
// }
// double num = dotProduct(n, a-c);
// t=-num/denom;
// return SKEW;
// }
#endif
Thanks |
| disruptivetech is offline | |
| | #2 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,769
| They're referring to each other, but thankfully they are using references. You can put "class Edge;" or "class Point;" in the headers before the respective classes (best use one) to make it work. The compiler must know another type before it parses it, so it must, for example, know Point before you use it. So Point must be defined before Edge. But if Edge also uses Point, then you must tell the compiler the type exists, but not necessarily define it (only need to if using it, so include it in the implementation).
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #3 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: class Point{
public:
double x;
double y;
Point(double _x = 0.0, double _y = 0.0);
Point operator+(Point&);
Point operator-(Point&);
friend Point operator*(double, Point&);
double operator[](int);
int operator==(Point&);
int operator!=(Point&);
int operator<(Point&);
int operator>(Point&);
int classify(Point&, Point&);
int classify(Edge&);
double polarAngle(void);
double length(void);
double distance(Edge&);
};
If so, you need to forward declare the second class, then define the first class itself. Something like this: Code: class Point;
#include "Edge.h"
class Point
{
...
};
A few other points: 1. Don't use filenames like "Edge.h" and "Point.h" - use lower-case only names, that will be much more convenient for portability. 2. Give names of variables in the declaration of member functions, e.g.: [code] Class Edge { ... intersect(Edge &e, double &t); }; It's not so important in these classes, but knowing the names of arguments in a function when looking at the class interface is really helpful, so it's a good habit. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #4 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,266
| A little off topic, but...
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #5 | |
| Registered User Join Date: Jul 2005
Posts: 41
| Quote:
Edge.cpp: In member function ‘Edge& Edge::rot()’: Edge.cpp:19: error: no match for ‘operator*’ in ‘5.0e-1 * ((Edge*)this)->Edge: rg. Point: perator+(((Point&)(&((Edge*)this)->Edge::dest)))’Point.h:10: note: candidates are: Point operator*(double, Point&) Edge.cpp:22: error: no match for ‘operator-’ in ‘m - operator*(5.0e-1, ((Point&)(& n)))’ Point.h:9: note: candidates are: Point Point: perator-(Point&)Edge.cpp:23: error: no match for ‘operator+’ in ‘m + operator*(5.0e-1, ((Point&)(& n)))’ Point.h:8: note: candidates are: Point Point: perator+(Point&)Edge.cpp: In member function ‘Point Edge: oint(double)’:Edge.cpp:34: error: no match for ‘operator*’ in ‘t * ((Edge*)this)->Edge::dest. Point: perator-(((Point&)(&((Edge*)this)->Edge: rg)))’Point.h:10: note: candidates are: Point operator*(double, Point&) Edge.cpp: In member function ‘int Edge::intersect(Edge&, double&)’: Edge.cpp:51: error: invalid initialisation of non-const reference of type ‘Point&’ from a temporary of type ‘Point’ Edge.cpp:39: error: in passing argument 2 of ‘double dotProduct(Point&, Point&)’ Edge.cpp:54: error: no matching function for call to ‘Point::classify(Edge&)’ Point.h:16: note: candidates are: int Point::classify(Point&, Point&) Edge.cpp:55: error: ‘LEFT’ was not declared in this scope Edge.cpp:55: error: ‘RIGHT’ was not declared in this scope Edge.cpp:60: error: invalid initialisation of non-const reference of type ‘Point&’ from a temporary of type ‘Point’ Edge.cpp:39: error: in passing argument 2 of ‘double dotProduct(Point&, Point&)’ make: *** [Edge.o] Error 1 Any ideas why this is failing? | |
| disruptivetech is offline | |
| | #6 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,769
| Add the appropriate include for Edge in the source file.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strange problem with classes in header files | samGwilliam | C++ Programming | 2 | 02-29-2008 04:55 AM |
| classes and header files | Drake | C++ Programming | 8 | 11-30-2006 07:12 PM |
| Learning how to use classes and create header files | Welshy | C++ Programming | 10 | 04-19-2005 12:33 PM |
| Header Files and Classes. | Lithorien | C++ Programming | 10 | 08-13-2004 12:10 PM |
| Using c++ standards | subdene | C++ Programming | 4 | 06-06-2002 09:15 AM |