本文介绍了错误:接收临时地址[-fpermissive]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了几个小时,但无济于事.基本上我有

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.

推荐答案

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

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++.

为什么 IsCollision()仍然采用指针?通过const引用获取其参数会更自然:

Why does IsCollision() take pointers anyway? It would be more natural to take its parameters by 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 any more
{
player1.score+=10;
stick1.x = rand() % 600+1;
stick1.y = rand() % 400+1;
play_sample(pickup,128,128,1000,false);
}

这篇关于错误:接收临时地址[-fpermissive]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:42