Vector Error

Okay, so I am to create a program for my OOP class that initializes an unknown number of rectangles by reading lines from a text file that contain an x value, a y value, a width, and a length. So, because I will not know the number of rectangle values within the file (and because my professor requires use of vector on this program), I must use a vector and push back each rectangle as their line is loaded from the file (x, y, width, length <nextline, repeat>). I have pretty much finished up my program, but I am getting an annoying error that I can't quite figure out. Below is my code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
class rectangle
{
public:
rectangle(int x, int y, int width, int length)
{
}
~rectangle()
{
delete &x;
delete &y;
delete &width;
delete &length;
}
bool intersects(rectangle &rect)
{
int refWidth = rect.getWidth();
int refLength = rect.getLength();

int bX1 = rect.getX();
int bY1 = rect.getY();
int bX2 = bX1+refWidth;
int bY2 = bY1+refLength;

int aX1 = x;
int aX2 = x+width;
int aY1 = y;
int aY2 = y+length;

if(aX1 < bX2 && aX2 > bX1
&&aY1 < bY2 && aY2 > bY1)
return true;
else
return false;
//determine if they cross each other
}
int getX()
{
return x;
}
int getY()
{
return y;
}
int getWidth()
{
return width;
}
int getLength()
{
return length;
}
private:
int x;
int y;
int width;
int length;
};
int main()
{
vector <rectangle> rect;
string temp;
int x, y, width, length;
bool hasIntersect;
cout<<"Loading..."<<endl;
ifstream inputFile("project2.txt");
if(!inputFile.is_open())
cout<<"Could not open file!"<<endl;
while(inputFile.good()) //load rectangles
{
inputFile>>temp;
x=atoi(temp.c_str());
inputFile>>temp;
y=atoi(temp.c_str());
inputFile>>temp;
width=atoi(temp.c_str());
inputFile>>temp;
length=atoi(temp.c_str());
rect.push_back(rectangle(x,y,width,length));
cout<<"Loaded rectangle.."<<endl;
}
for(int i=0;i<rect.size();i++)
{
hasIntersect=false;
cout<<"Rectangle "<<i<<" intersects with ";
for(int j=0;j<rect.size();j++)
{
if(i!=j) //don't check if a rectangle intersects itself
{
if(rect[i].intersects(rect[j]))
{
hasIntersect=true;
cout<<j<<" ";
}
}
}
if(!hasIntersect)
cout<<"nothing."<<endl;
else
cout<<endl;
}
system("pause");
return 0;
}

I'm using MSVC++ Express. When stepping through the program, I encounter the error at "rect.push_back(rectangle(x,y,width,length));". The error says "Debug Assertation Failed!" I believe this has something to do with a pointer, but I can't figure out how to get it to work. I read that I am supposed to use vector<rectangle*> rect instead of vector <rectangle> rect, but then I get all sorts of compiler errors and I can't figure out why. Thank you so much for your help.
-Andrew
You should only use delete on things you created with new, so get rid of all the deletes in the destructor.
By the way, your rectangle class doesn't actually seem to have any member variables.
it has variables:
"private:
int x;
int y;
int width;
int length;"
Okay, so the errors stopped when I removed those from the destructor. But it is now saying that none of the rectangles from the file intercept when I know some of them do. Here is the input file:
12 30 50 50
02 02 20 40
33 33 02 02
00 00 15 15
99 99 01 01


Is the logic right?

bool intersects(rectangle &rect)
{
int refWidth = rect.getWidth();
int refLength = rect.getLength();

int bX1 = rect.getX();
int bY1 = rect.getY();
int bX2 = bX1+refWidth;
int bY2 = bY1+refLength;

int aX1 = x;
int aX2 = x+width;
int aY1 = y;
int aY2 = y+length;

if(aX1 < bX2 && aX2 > bX1
&&aY1 < bY2 && aY2 > bY1)
return true;
else
return false;
//determine if they cross each other
}
And the getters are minimized. All they do is return the value of their paired member variable. So pay them no mind.
In the constructor you don't initialize the member variables
Topic archived. No new replies allowed.