Error: taking address of temporary [-fpermissive]

后端 未结 3 887
生来不讨喜
生来不讨喜 2021-02-04 05:36

I\'ve been looking into this for a few hours, to no avail. Basically I have

struct rectangle {
    int x, y, w, h;
};

rectangle player::RegionCoordinates() // R         


        
3条回答
  •  囚心锁ツ
    2021-02-04 05:40

    Since IsCollision takes a rectangle * and you are taking the address of the result here:

    if (IsCollision(&player1.RegionCoordinates(), &stick1.RegionCoordinates()))
    

    You most likely are returning a rectangle back from RegionCoordinates() which is a temporary variable since it will disappear after the if statement is done. If you assign the result of RegionCoordinates() to a variable then it will no longer be a temporary and you can then take the address of it:

    rectangle r1 = player1.RegionCoordinates() ;
    rectangle r2 = stick1.RegionCoordinates() ;
    if (IsCollision(&r1, &r2))
    

    Alternatively you could take the parameters as const references which would be the more C++ way of doing it:

    bool IsCollision (const rectangle &r1, const rectangle  &r2)
    

提交回复
热议问题