The error is listed in the title and then commented in main (last comment). The program runs but once it encounters the bool expression I overloaded, it seems as if the program pauses.

I have a feeling it's a stack overload due to the way my function operates, but I hope I'm wrong.

Here's the code--

#include <cstdlib>
#include <iostream>
#include <math.h>

/**
File test_!.cpp in Project testing_Something.dev

Progress of project: Unexpected System exit after a certain amount of time, but no errors.
Suspecting the stack ran out of room to store variables for vector-magnitude comparisons via
the overloaded operator ==.

So far tests show that this project was successful, but I'm still trying to find the error.
*/

using namespace std;

class Point;
class Triangle;
class P2DVector;

class Point{     
      private:
             double xP;
             double yP;
             
      public:
             Point(double x, double y){
                   xP = x;
                   yP = y;
             };
             double getX(){return xP;};
             double getY(){return yP;};
             void showCoord(){
                  cout << getX() << ", " << getY() << endl;
             };
};

class P2DVector{
      private:
             Point *points[2]; 
      
      public:
             P2DVector(Point *first, Point *second){
                      points[0] = first;
                      points[1] = second;        
             };
             ~P2DVector(){for(int i = 0; i < 2; i++){ if(points[i] != 0){delete points[i]; points[i] = 0;}}}; 
             static double power(double value, int exponent){
                    if(exponent < 0)
                    {
                        cout << "Negative powers currently unsupported.";
                        cout << "\nRetrying with positive value." << endl;
                        return power(value, -exponent);
                    }
                    else if(exponent == 0)
                        return 1;
                    else
                        return value * power(value, exponent - 1);
             };
             /*
             static inline double root(double value, int root)
             {
                    double temp = 1.0;
                    
                    for(double i = 0.000001; i < 10; i += .000001)
                    {
                         for(int j = 0; j < root; j++)
                              temp *= i;
                         
                         if(static_cast<long>(temp) == value)
                             return i;
                             
                         temp = 1.0;
                    }
                    return 0.0;
             };*/ //not precise enough, and even when it is, it's just too slow
             double getXDir(){
                    return (points[1]->getX() - points[0]->getX());
             };
             double getYDir(){
                    return (points[1]->getY() - points[0]->getY());
             };
             double magnitude(){
                    return sqrt( (power( points[1]->getX() - points[0]->getX() ,2) 
                                 +power(points[1]->getY() - points[0]->getY() ,2)));
             };
             Point startPoint(){
                   Point p (points[0]->getX(), points[0]->getY());
                   return p;
             };
             Point endPoint(){
                   Point p (points[1]->getX(), points[1]->getY());
                   return p;
             };
             P2DVector *unitP2DVector(){      
                    Point *unitPoint[2];
                    
                    unitPoint[0] = new Point(points[0]->getX() / magnitude(), points[0]->getY() / magnitude());
                    unitPoint[1] = new Point(points[1]->getX() / magnitude(), points[1]->getY() / magnitude());
                    
                    P2DVector *temp = new P2DVector(unitPoint[0], unitPoint[1]);
                    
                    return temp;
             };
             void displayLocation(){
                  cout << "This P2DVector Starts-> " << points[0]->getX() << ", " << points[0]->getY();
                  cout << "\nEnds-] " << points[1]->getX() << ", " << points[1]->getY();
                  cout << "\nDirection is <" << getXDir() << ", " << getYDir() <<">";
                  cout << ".\nContains magnitude: " << magnitude() << "\n" << endl;
             };
};

class Triangle{
      private:
              Point *coordinates[3], *shortestCollision[3];
              P2DVector *innerVectors[3], *outterVectors[3];
      
      public:
             Triangle(double xLoc, double yLoc, Point *points[3]){         
                  for(int i = 0; i < 3; i++)
                        coordinates[i] = new Point( xLoc + points[i]->getX(), yLoc + points[i]->getY());    
                                          
                  //need the natural vectors of the triangle!
                  outterVectors[0] = new P2DVector(coordinates[0], coordinates[1]);
                  outterVectors[1] = new P2DVector(coordinates[1], coordinates[2]);
                  outterVectors[2] = new P2DVector(coordinates[2], coordinates[0]);
                  
                  //need vectors for opposite sides of points!
                  shortestCollision[0] = new Point(outterVectors[1]->startPoint().getX() + ((outterVectors[1]->getXDir() ) / 2),
                                                   outterVectors[1]->startPoint().getY() + ((outterVectors[1]->getYDir() ) / 2));
                  innerVectors[0] = new P2DVector(coordinates[0], shortestCollision[0]);
                  
                  shortestCollision[1] = new Point(outterVectors[2]->startPoint().getX() + ((outterVectors[2]->getXDir() ) / 2),
                                                   outterVectors[2]->startPoint().getY() + ((outterVectors[2]->getYDir() ) / 2));
                  innerVectors[1] = new P2DVector(coordinates[1], shortestCollision[1]);
                  
                  shortestCollision[2] = new Point(outterVectors[0]->startPoint().getX() + ((outterVectors[0]->getXDir() ) / 2),
                                                   outterVectors[0]->startPoint().getY() + ((outterVectors[0]->getYDir() ) / 2));
                  innerVectors[2] = new P2DVector(coordinates[2], shortestCollision[2]);                                 
                  
             };                                    
             ~Triangle(){
                  for(int i = 0; i < 3; i++)
                  {
                        innerVectors[i]->P2DVector::~P2DVector();
                        outterVectors[i]->P2DVector::~P2DVector();
                  }
             };
             void displayParameters(){
                  cout << "This triangle is defined by the points/vectors (in this order):\n" << flush;
                  
                  for(int i = 0; i < 3; i++)
                  {
                          coordinates[i]->showCoord();
                          cout << "\nWith the inner vector:\n";
                          innerVectors[i]->displayLocation();
                          cout << "\n" << flush;
                  }
             };
             Point getFirstPoint(){
                   return coordinates[0][0];
             };
             Point getSecondPoint(){
                   return coordinates[1][0];
             };
             Point getThirdPoint(){
                   return coordinates[2][0];
             }; 
             bool operator== (Triangle other){
                  Point rogueTrianglePts[3] = {other.getFirstPoint(), other.getSecondPoint(), other.getThirdPoint()};
                  Point thisTrianglePts[3] = {getFirstPoint(), getSecondPoint(), getThirdPoint()};     
                  for(int i = 0; i < 3; i++){
                          for(int j = 0; j < 3; j++){
                             P2DVector rogueVector (&thisTrianglePts[j], &rogueTrianglePts[i]);
                             
                            if(
                                  rogueVector.magnitude() <= innerVectors[j]->magnitude()
                              )
                              {
                                 cout << "There was an intersection!" << endl;
                                 return true;
                              }
                          }
                  }
                  cout << "There was no intersection!" << endl;
                  return false;
             };                                 
};

int main(int argc, char *argv[]){
    
    Point *myPoints[3], *otherPoints[3], *anotherOutsider[3];
    
    myPoints[0] = new Point(2, 0);//be careful using unions! You need to set properties for them if
    myPoints[1] = new Point(4, 1);//you are using blank variables?...
    myPoints[2] = new Point(2, 3);
    
    otherPoints[0] = new Point(0, 0);
    otherPoints[1] = new Point(1, 0);
    otherPoints[2] = new Point(1, 1);
    
    anotherOutsider[0] = new Point(-2, 2);
    anotherOutsider[1] = new Point(0, 0);
    anotherOutsider[2] = new Point(-2, 0);
    
    Triangle *tri = new Triangle(1, 1, myPoints), *otherTri = new Triangle(5, 2, otherPoints),
    *lastTriangle = new Triangle(4, 0, anotherOutsider);
    
    tri->displayParameters();
    otherTri->displayParameters();
    lastTriangle->displayParameters();
    
    /*
    cout << ((*tri)==(*otherTri)) << endl; //Problem occurs here. Suspecting a stack-overload issue.
    cout << ((*tri)==(*lastTriangle)) << endl;
    cout << ((*otherTri)==(*lastTriangle)) << endl;
    */
    
    /*
    tri->Triangle::~Triangle();
    otherTri->Triangle::~Triangle();
    lastTriangle->Triangle::~Triangle();
    */
    
    /*
    delete tri;
    delete otherTri;
    delete lastTriangle;
    */
    
    cin.get();
    return 0;
}

Recommended Answers

All 4 Replies

The problem(s) appear to be the use of all those pointers. Get rid of all the pointers and you get rid of the errors. Here's the revised program without pointers. This implementation also deleted the destructors, new and delete operators.

#include <cstdlib>
#include <iostream>
#include <math.h>

/**
File test_!.cpp in Project testing_Something.dev

Progress of project: Unexpected System exit after a certain amount of time, but no errors.
Suspecting the stack ran out of room to store variables for vector-magnitude comparisons via
the overloaded operator ==.

So far tests show that this project was successful, but I'm still trying to find the error.
*/

using namespace std;

class Point;
class Triangle;
class P2DVector;

class Point{     
      private:
             double xP;
             double yP;
             
      public:
             Point(double x, double y){
                   xP = x;
                   yP = y;
             };
             Point() {xP = 0; yP = 0;}
             void Set(double x,double y) {xP = x; yP = y;}
             double getX(){return xP;};
             double getY(){return yP;};
             void showCoord(){
                  cout << getX() << ", " << getY() << endl;
             };
};

class P2DVector{
      private:
             Point points[2]; 
      
      public:
             P2DVector(Point& first, Point& second){
                      points[0] = first;
                      points[1] = second;        
             };
             P2DVector() {points[0] = Point(0,0); points[1] = Point(0,0); }
             void Set(Point& first, Point& second){
                      points[0] = first;
                      points[1] = second;        
             };
             //~P2DVector(){for(int i = 0; i < 2; i++){ if(points[i] != 0){delete points[i]; points[i] = 0;}}}; 
             static double power(double value, int exponent){
                    if(exponent < 0)
                    {
                        cout << "Negative powers currently unsupported.";
                        cout << "\nRetrying with positive value." << endl;
                        return power(value, -exponent);
                    }
                    else if(exponent == 0)
                        return 1;
                    else
                        return value * power(value, exponent - 1);
             };
             /*
             static inline double root(double value, int root)
             {
                    double temp = 1.0;
                    
                    for(double i = 0.000001; i < 10; i += .000001)
                    {
                         for(int j = 0; j < root; j++)
                              temp *= i;
                         
                         if(static_cast<long>(temp) == value)
                             return i;
                             
                         temp = 1.0;
                    }
                    return 0.0;
             };*/ //not precise enough, and even when it is, it's just too slow
             double getXDir(){
                    return (points[1].getX() - points[0].getX());
             };
             double getYDir(){
                    return (points[1].getY() - points[0].getY());
             };
             double magnitude(){
                    return sqrt( (power( points[1].getX() - points[0].getX() ,2) 
                                 +power(points[1].getY() - points[0].getY() ,2)));
             };
             Point startPoint(){
                   Point p(points[0].getX(), points[0].getY());
                   return p;
             };
             Point endPoint(){
                   Point p(points[1].getX(), points[1].getY());
                   return p;
             };
             P2DVector unitP2DVector(){      
                    Point unitPoint[2];
                    
                    unitPoint[0].Set(points[0].getX() / magnitude(), points[0].getY() / magnitude());
                    unitPoint[1].Set(points[1].getX() / magnitude(), points[1].getY() / magnitude());
                    
                    P2DVector temp(unitPoint[0], unitPoint[1]);
                    
                    return temp;
             };
             void displayLocation(){
                  cout << "This P2DVector Starts-> " << points[0].getX() << ", " << points[0].getY();
                  cout << "\nEnds-] " << points[1].getX() << ", " << points[1].getY();
                  cout << "\nDirection is <" << getXDir() << ", " << getYDir() <<">";
                  cout << ".\nContains magnitude: " << magnitude() << "\n" << endl;
             };
};

class Triangle{
      private:
              Point coordinates[3], shortestCollision[3];
              P2DVector innerVectors[3], outterVectors[3];
      
      public:
             Triangle(double xLoc, double yLoc, Point points[3]){         
                  for(int i = 0; i < 3; i++)
                        coordinates[i].Set( xLoc + points[i].getX(), yLoc + points[i].getY());    
                                          
                  //need the natural vectors of the triangle!
                  outterVectors[0].Set(coordinates[0], coordinates[1]);
                  outterVectors[1].Set(coordinates[1], coordinates[2]);
                  outterVectors[2].Set(coordinates[2], coordinates[0]);
                  
                  //need vectors for opposite sides of points!
                  shortestCollision[0].Set(outterVectors[1].startPoint().getX() + ((outterVectors[1].getXDir() ) / 2),
                                                   outterVectors[1].startPoint().getY() + ((outterVectors[1].getYDir() ) / 2));
                  innerVectors[0].Set(coordinates[0], shortestCollision[0]);
                  
                  shortestCollision[1].Set(outterVectors[2].startPoint().getX() + ((outterVectors[2].getXDir() ) / 2),
                                                   outterVectors[2].startPoint().getY() + ((outterVectors[2].getYDir() ) / 2));
                  innerVectors[1].Set(coordinates[1], shortestCollision[1]);
                  
                  shortestCollision[2].Set(outterVectors[0].startPoint().getX() + ((outterVectors[0].getXDir() ) / 2),
                                                   outterVectors[0].startPoint().getY() + ((outterVectors[0].getYDir() ) / 2));
                  innerVectors[2].Set(coordinates[2], shortestCollision[2]);                                 
                  
             };                                    
             void displayParameters(){
                  cout << "This triangle is defined by the points/vectors (in this order):\n" << flush;
                  
                  for(int i = 0; i < 3; i++)
                  {
                          coordinates[i].showCoord();
                          cout << "\nWith the inner vector:\n";
                          innerVectors[i].displayLocation();
                          cout << "\n" << flush;
                  }
             };
             Point getFirstPoint(){
                   return coordinates[0];
             };
             Point getSecondPoint(){
                   return coordinates[1];
             };
             Point getThirdPoint(){
                   return coordinates[2];
             }; 
             bool operator== (Triangle other){
                  Point rogueTrianglePts[3] = {other.getFirstPoint(), other.getSecondPoint(), other.getThirdPoint()};
                  Point thisTrianglePts[3] = {getFirstPoint(), getSecondPoint(), getThirdPoint()};     
                  for(int i = 0; i < 3; i++){
                          for(int j = 0; j < 3; j++){
                             P2DVector rogueVector (thisTrianglePts[j], rogueTrianglePts[i]);
                             
                            if(
                                  rogueVector.magnitude() <= innerVectors[j].magnitude()
                              )
                              {
                                 cout << "There was an intersection!" << endl;
                                 return true;
                              }
                          }
                  }
                  cout << "There was no intersection!" << endl;
                  return false;
             };                                 
};

int main(int argc, char *argv[]){
    
    Point myPoints[3];
    Point otherPoints[3];
    Point anotherOutsider[3];
    
    myPoints[0].Set(2, 0);//be careful using unions! You need to set properties for them if
    myPoints[1].Set(4, 1);//you are using blank variables?...
    myPoints[2].Set(2, 3);
    
    otherPoints[0].Set(0, 0);
    otherPoints[1].Set(1, 0);
    otherPoints[2].Set(1, 1);
    
    anotherOutsider[0].Set(-2, 2);
    anotherOutsider[1].Set(0, 0);
    anotherOutsider[2].Set(-2, 0);
    
    Triangle tri(1, 1, myPoints);
    Triangle otherTri(5, 2, otherPoints);
    Triangle lastTriangle(4, 0, anotherOutsider);
    
//    tri.displayParameters();
//    otherTri.displayParameters();
//    lastTriangle.displayParameters();
    cout << (tri == otherTri) << endl; //Problem occurs here. Suspecting a stack-overload issue.
    cout << (tri==lastTriangle) << endl;
    cout << (otherTri==lastTriangle) << endl;
    
//    delete tri;
//    delete otherTri;
//    delete lastTriangle;
    
    cin.get();
    return 0;
}

The problem(s) appear to be the use of all those pointers. Get rid of all the pointers and you get rid of the errors. Here's the revised program without pointers. This implementation also deleted the destructors, new and delete operators.

To tell you the truth I was purposely trying to use pointers since they're very adjacent to Java's reference variables.

I didn't realize they could cause so many problems!

The first time I attempted this project I tried using unions instead of classes (because I expected it to be more efficient to use a structure that I wasn't planning on deriving or being the base of anything) and I ran into some issues with how memory is assigned to the unions and my variables kept getting overwritten!

But now this... however I'm fairly confused as to how using pointers so much can be bad, especially if you can find a way to handle them through your program? I didn't think I was using too much space honestly.

>>To tell you the truth I was purposely trying to use pointers since they're very adjacent to Java's reference variables
I think you misunderstood the relationship. From what I understand Java doesn't have the concept of a pointer like what is in c++ pointers. The reference variables in Java are equivilant to the referneces in C++, not pointers. See line 45 of the code I posted.

>>I didn't realize they could cause so many problems!
Yup -- try to avoid pointers when ever possible.

>>I expected it to be more efficient to use a structure
No. In C++ structures are nearly identical to classes. There is no efficiency difference between the two.


>>I didn't think I was using too much space honestly.
It wasn't the space but the complexity of pointers. You just simply made your program too complex with all those pointers. And I didn't test all the pointers but it is pretty easy to use uninitialized pointers or pointers with NULL values. And either of those conditions will cause the program to scribble all over memory.

The fewer pointers you use the fewer bugs you have to debug.

>>To tell you the truth I was purposely trying to use pointers since they're very adjacent to Java's reference variables
I think you misunderstood the relationship. From what I understand Java doesn't have the concept of a pointer like what is in c++ pointers. The reference variables in Java are equivilant to the referneces in C++, not pointers. See line 45 of the code I posted.

In java the "reference variables" can be thought of as either references or pointers because you can dynamically allocate memory to these "reference variables" or assign them the address of another value... or the value will be automatically unboxed if it is a class type (that is the class representation of a primitive type) thrown into a primitive type, and vice versa.

The only differences I've seen is that you have no access to the stack in java like you do in c++ (since all reference-variables handle data in the heap) and that you can't use incremental operations on the java "reference variables."

It's part of the reason I felt comfortable with using pointers, but next time I'll be more cautious and take the time to study between when to use c++ reference-variables and pointers.

Thanks a million.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.