LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Class problems (https://www.linuxquestions.org/questions/programming-9/class-problems-637027/)

disruptive 04-22-2008 08:41 AM

Class problems
 
These classes are published classes, simply I am using them now, but cannot even get them to compile. So for example I get the following errors relating to member functions not being found. I have simplified the code such that one can see the point of failure. Does anyone know what is happening here to cause the failure.

Code:

Code:

Edge.cpp
========


#include "Point.h"
#include "Edge.h"


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;
  m = 0.5 * (org + dest);
  Point v = dest - org;
  Point n(v.y, -v.x);
  org = m - 0.5 * n;
  dest = m + 0.5 * n;
  return *this;
}



Edge.h file
========



class Point;

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);
 };


Point.cpp
========



#include "Point.h"

Point::Point(double _x, double _y):
x(_x), y(_y)
{
}

Point Point::operator+(Point &p)
{
  return Point(x+p.x, y+p.y);
}

Point Point::operator-(Point &p)
{
  return Point(x - p.x, y-p.y);
}

Point operator*(double s, Point &p)
{
  return Point(s *p.x, s *p.y);
}

double Point::operator[](int i)
{
  return (i ==0) ? x : y;
}

int Point::operator==(Point &p)
{
  return (x == p.x) && (y == p.y);
}

int Point::operator!=(Point &p)
{
  return !(*this == p);
}

int Point::operator<(Point &p)
{
  return ((x < p.x) || ((x == p.x) && (y < p.y)));
}

int Point::operator>(Point &p)
{
  return ((x > p.x) || ((x == p.x) && (y > p.y)));
}


/*int main(void)
{

return (0);
}*/



Point.h
========

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&);
};

Error on compilation.
============

g++ -c Edge.cpp -o Edge.o
Edge.cpp: In member function ‘Edge& Edge::rot()’:
Edge.cpp:20: 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:23: 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:24: error: no match for ‘operator+’ in ‘m + operator*(5.0e-1, ((Point&)(& n)))’
Point.h:8: note: candidates are: Point Point:perator+(Point&)

Code:

The Makefile is as follows:


all: Poly
       
Poly:    Point.o Edge.o Polygons.o

        g++  -o Poly Point.o Edge.o Polygons.o

Point.o: Point.cpp
        g++    -c Point.cpp -o Point.o       

Edge.o: Edge.cpp
        g++    -c Edge.cpp -o Edge.o       
       
Polygons.o: Polygons.cpp
        g++    -c Polygons.cpp -o Polygons.o

clean:
        find . -name '*.o' | xargs rm -f ;

Thanks

ntubski 04-22-2008 11:31 AM

The problem is that the operator functions take a Point&. This is Point reference, which means that the function can modify the variables passed to it. In this statement
Code:

0.5 * (org + dest);
you pass an expression to the operator* function. An expression can't be modified. Declaring the functions with const references works:
Code:

friend Point operator*(double, const Point&);


All times are GMT -5. The time now is 02:16 AM.