C++ 错误:取临时地址 [-fpermissive]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16481490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:23:28  来源:igfitidea点击:

Error: taking address of temporary [-fpermissive]

c++collision-detectioncollisionallegro

提问by Hyman Riales

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() // 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 something really obvious but for the life of me I can't figure it out.

有任何想法吗?我敢肯定这是非常明显的事情,但对于我的生活,我无法弄清楚。

回答by Angew is no longer proud of SO

RegionCoordinates()returns an object by value. This means a call to RegionCoordinates()returns a temporary instance of rectangle. As the error says, you're trying to take the address of this temporary object, which is not legal in C++.

RegionCoordinates()按值返回一个对象。这意味着调用RegionCoordinates()返回 的临时实例rectangle。正如错误所说,您正在尝试获取此临时对象的地址,这在 C++ 中是不合法的。

Why does IsCollision()take pointers anyway? It would be more natural to take its parameters by const reference:

为什么IsCollision()无论如何都要接受指针?通过 const 引用获取其参数会更自然:

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 any more
{
player1.score+=10;
stick1.x = rand() % 600+1;
stick1.y = rand() % 400+1;
play_sample(pickup,128,128,1000,false);
}

回答by Shafik Yaghmour

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

由于IsCollision需要 arectangle *并且您在此处获取结果的地址:

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

You most likely are returning a rectangleback from RegionCoordinates()which is a temporary variable since it will disappear after the ifstatement 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回从RegionCoordinates()它是一个临时变量,因为以后会消失if语句完成。如果您将 的结果分配RegionCoordinates()给一个变量,那么它将不再是临时的,您可以获取它的地址:

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

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

或者,您可以将参数作为const参考,这将是更多的 C++ 方式:

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

回答by Andy Prowl

Given the kind of error you are getting, I must assume RegionCoordinates()is returning an object by value, thus causing the creation of a temporary, and you are taking the address of that temporary.

鉴于您遇到的错误类型,我必须假设RegionCoordinates()是按值返回一个对象,从而导致创建一个临时,并且您正在获取该临时的地址。

The address-of operator requires an lvalueas its operand, but you are applying it to an rvalue(temporaries are rvalues).

address-of 运算符需要一个左值作为其操作数,但您将其应用于右值(临时rvalues)。

You could do this (if you are not using C++11, replace autowith the type returned by RegionCoordinates):

您可以这样做(如果您不使用 C++11,请替换auto为 返回的类型RegionCoordinates):

auto rcPlayer1 = player1.RegionCoordinates();
auto rcStick1 = player1.RegionCoordinates();
if (IsCollision(&rcPlayer1, &rcStick1)) //ERROR
{
    player1.score+=10;
    stick1.x = rand() % 600+1;
    stick1.y = rand() % 400+1;
    play_sample(pickup,128,128,1000,false);
}

Alternatively, you can change IsCollisionso that it accepts referencesrather than pointers, as suggested by Angew in his answer.

或者,您可以进行更改IsCollision,使其接受引用而不是指针,正如 Angew 在他的回答中所建议的那样