c++ - Error: taking address of temporary [-fpermissive] -
i've been looking few hours, no avail. have
struct rectangle { int x, y, w, h; }; rectangle player::regioncoordinates() // region coord { rectangle temp; temp.x = colregion.x + coordinates.x; temp.w = colregion.w; temp.y = colregion.y + coordinates.y; temp.h = colregion.h; return temp; } // collision detect function bool iscollision (rectangle * r1, rectangle * r2) { if (r1->x < r2->x + r2->w && r1->x + r1->w > r2->x && r1->y < r2->y + r2->h && r1->y + r1->h > r2->y) { return true; } return false; } //blah blah main while loop if (iscollision(&player1.regioncoordinates(), &stick1.regioncoordinates())) //error { player1.score+=10; stick1.x = rand() % 600+1; stick1.y = rand() % 400+1; play_sample(pickup,128,128,1000,false); } any ideas? i'm sure it's obvious life of me can't figure out.
regioncoordinates() returns object value. means call regioncoordinates() returns temporary instance of rectangle. error says, you're trying take address of temporary object, not legal in c++.
why iscollision() take pointers anyway? more natural take parameters const reference:
bool iscollision (const rectangle &r1, const rectangle &r2) { if (r1.x < r2.x + r2.w && r1.x + r1.w > r2.x && r1.y < r2.y + r2.h && r1.y + r1.h > r2.y) { return true; } return false; } //blah blah main while loop if (iscollision(player1.regioncoordinates(), stick1.regioncoordinates())) //no error more { player1.score+=10; stick1.x = rand() % 600+1; stick1.y = rand() % 400+1; play_sample(pickup,128,128,1000,false); }
Comments
Post a Comment