I am compiling my program and it is compiling fine; however, i go to run it and the first function call it crashes out. I ran the debugger and looking under my X and Y declarations under the debugger it says that the "Error: expression cannot be evaluated" for both X and Y:
I believe the error is occurring under the twoPoint.h where the set functions is implemented:
Here is my code:
MAIN:
POINT . H , which has the virtual functions:Code:#include <iostream> #include <math.h> #include "twoPoint.h" using namespace std; int main () { twoPoint<double> origin (0,0); // test overloaded constructor twoPoint<double> p1; // test default constructor p1.set(3,4); // test set function twoPoint<double> p2 (4,3); twoPoint<double> p3; p3 = origin; // test assignment operator p1.distance(p2); p1.distance(p3); return 0; }
Here is my TwoPoint.hCode:/* Name: Date: CS132 Spring 2005 Program Description: */ #ifndef point_h #define point_h #include <iostream> using namespace std; // BASE class Point template <class TYPE> class Point { protected: TYPE* axis; int dimensions; public: Point(); Point(int dimensions); Point(Point& p); ~Point(); Point& operator= (Point& p); virtual void set(); virtual TYPE distance(Point& p); virtual void print() const; }; #endif template <class TYPE> Point<TYPE>::Point() { dimensions = 0; axis = new TYPE[dimensions]; } template <class TYPE> Point<TYPE>::Point(int dimensions) { this->dimensions = dimensions; axis = new TYPE[dimensions]; for (int i = 0; i < dimensions; i++) axis[i] = 0; } template <class TYPE> Point<TYPE>::Point(Point& p) { this->dimensions = p.dimensions; this->axis = new TYPE[dimensions]; for (int i = 0; i < dimensions; i++) this->axis[i] = p.axis[i]; return *this; } template <class TYPE> Point<TYPE>::~Point() { delete [] axis; } template <class TYPE> Point<TYPE>& Point<TYPE>::operator= (Point& p) { ~Point(); this->dimensions = p.dimensions; this->axis = new TYPE[dimensions]; for (int i = 0; i < dimensions; i++) this->axis[i] = p.axis[i]; return *this; } // virtual - pure template <class TYPE> void Point<TYPE>::set() {} template <class TYPE> TYPE Point<TYPE>::distance(Point& p) { TYPE dist = 0; for (int i = 0; i < dimensions; i++) dist += (axis[i]-p.axis[i]) * (axis[i]-p.axis[i]); dist = sqrt(dist); cout << "distance from "; print(); cout << " to "; p.print(); cout << " = " << dist << endl; return dist; } template <class TYPE> void Point<TYPE>::print() const { cout << "("; if (dimensions == 0) { cout << "\bdimensionless point" << endl; return; } for (int i = 0; i < dimensions; i++) cout << axis[i] << ", "; cout << "\b\b)"; }
Code:#ifndef twoPoint_h #define twoPoint_h #include <iostream> #include <math.h> #include "point.h" using namespace std; template <class TYPE> class twoPoint : public Point<TYPE> { private: TYPE *x, *y; public: twoPoint(); //default twoPoint (twoPoint<TYPE> &that); // Copy ~twoPoint(); // destructor twoPoint &operator = (twoPoint<TYPE> &that); //Assignment Operator twoPoint (TYPE x, TYPE y); // Overload Constructor void set (TYPE x, TYPE y); // set function }; #endif template <class TYPE> twoPoint<TYPE>::twoPoint() : Point() // Call original Point Constructor // default { *x = axis[0]; *y = axis[1]; } template <class TYPE> twoPoint<TYPE>::twoPoint(TYPE x, TYPE y) { set(x, y); } template <class TYPE> void twoPoint<TYPE>::set(TYPE x, TYPE y) // set function { *(this -> x) = x; *(this -> y) = y; } template <class TYPE> twoPoint<TYPE>::twoPoint(twoPoint &that) // Copy { this -> x = new double; *(this -> x) = *that.x; this -> y = new double; *(this -> y) = *that.y; } template <class TYPE> // destructor twoPoint<TYPE>::~twoPoint() { delete x; delete y; } template <class TYPE> twoPoint<TYPE>& twoPoint <TYPE>:: operator = (twoPoint &that) { this -> x = that.x; this -> y = that.y; return (*this); }


Reply With Quote