EDN Admin
Well-known member
Im not sure what this error means. It says I have an invalid NULL pointer, which I understand to mean theres a pointer in there which hasnt been set before being used. However I havent used any pointers at all.
Some of it isnt mine, Im just testing the check_collision() function because the tutorial I was reading provided it, and I dont understand how it works. According to my prediction, it should return true, meaning a collision has been found when there hasnt.
Heres the code:
<pre class="prettyprint #include<iostream>
#include<vector>
#include<string>
using namespace std;
class MyRect {
private:
public:
int x,y;
int w,h;
}A, B;
bool check_collision();
int main () {
char yn = n;
string mystr = false;
Label:
cout << "Enter a number for As X coordinate." ;
cin >> A.x;
cout << "Enter a number for As Y coordinate." ;
cin >> A.x;
cout << "Enter a number for As width." ;
cin >> A.w;
cout << "Enter a number for As height." ;
cin >> A.h;
cout << "Enter a number for Bs X coordinate." ;
cin >> B.x;
cout << "Enter a number for Bs Y coordinate." ;
cin >> B.x;
cout << "Enter a number for Bs width." ;
cin >> B.w;
cout << "Enter a number for Bs height." ;
cin >> B.h;
cout << "Checking box collisions.";
if (check_collision() == 1 ) { mystr = "false"; }
else if (check_collision() == 0) {mystr = "true";}
cout << "The results were: " << mystr << " Would you like to run it again?n";
cin >> yn;
if (yn == y) {
goto Label;
}
cin >> A.x;
return 0;
}
bool check_collision() { //The sides of the rectangles
int leftA,leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go through the A boxes
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Go through the B boxes
//Calculate the sides of rect B
leftB = A.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If no sides from A are outside of B
if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false ) {
//A collision is detected
return true; }
//If neither set of collision boxes touched
return false;
} Any help would be appreciated!
[/code]
<br/>
View the full article
Some of it isnt mine, Im just testing the check_collision() function because the tutorial I was reading provided it, and I dont understand how it works. According to my prediction, it should return true, meaning a collision has been found when there hasnt.
Heres the code:
<pre class="prettyprint #include<iostream>
#include<vector>
#include<string>
using namespace std;
class MyRect {
private:
public:
int x,y;
int w,h;
}A, B;
bool check_collision();
int main () {
char yn = n;
string mystr = false;
Label:
cout << "Enter a number for As X coordinate." ;
cin >> A.x;
cout << "Enter a number for As Y coordinate." ;
cin >> A.x;
cout << "Enter a number for As width." ;
cin >> A.w;
cout << "Enter a number for As height." ;
cin >> A.h;
cout << "Enter a number for Bs X coordinate." ;
cin >> B.x;
cout << "Enter a number for Bs Y coordinate." ;
cin >> B.x;
cout << "Enter a number for Bs width." ;
cin >> B.w;
cout << "Enter a number for Bs height." ;
cin >> B.h;
cout << "Checking box collisions.";
if (check_collision() == 1 ) { mystr = "false"; }
else if (check_collision() == 0) {mystr = "true";}
cout << "The results were: " << mystr << " Would you like to run it again?n";
cin >> yn;
if (yn == y) {
goto Label;
}
cin >> A.x;
return 0;
}
bool check_collision() { //The sides of the rectangles
int leftA,leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Go through the A boxes
//Calculate the sides of rect A
leftA = A.x;
rightA = A.x + A.w;
topA = A.y;
bottomA = A.y + A.h;
//Go through the B boxes
//Calculate the sides of rect B
leftB = A.x;
rightB = B.x + B.w;
topB = B.y;
bottomB = B.y + B.h;
//If no sides from A are outside of B
if( ( ( bottomA <= topB ) || ( topA >= bottomB ) || ( rightA <= leftB ) || ( leftA >= rightB ) ) == false ) {
//A collision is detected
return true; }
//If neither set of collision boxes touched
return false;
} Any help would be appreciated!
[/code]
<br/>
View the full article