Reputation: 1
I'm attempting to write a C++ program that uses a Point class consisting of x and y coordinates (ints) and a boolean. The x and y coordinates should be in the range from 0-100, but upon execution and with a print statement to test, the values printed out are corrupted.
My code looks like this:
int main(int argc, const char * argv[]) {
vector<Point> points;
for (int i = 0; i<200; i++) {
int rand_X = (rand() % 100) + 1;
int rand_Y = (rand() & 100) + 1;
Point point = Point(rand_X, rand_Y, false);
cout<< point.getX() << " " << point.getY() << endl;
points.push_back(point);
}
return 0;
and the Point class looks like this, if that's relevant:
struct Point {
public:
int x;
int y;
bool in_layer;
Point(int x, int y, bool in_layer) {}
int getX() {return this->x;}
int getY() {return this->y;}
};
And the output is just "-272632592 32766" repeated 200 times, which I'm assuming is a corrupted value.
Any ideas what could be causing the issue?
Upvotes: 0
Views: 81
Reputation: 1388
Your constructor didn't do anything, which contribute to an UB when you call getX
getY
methods. Plus, I think there's no need to use constructor and getx
gety
methods here.
If that's a code for learning, you may need to assign to the member x, y
in the constructor or use the folowing technique (initialization list):
Point(int _x, int _y, bool _in_layer) : x(_x), y(_y), in_layer(_in_layer){}
Upvotes: 1