c++ - 神秘的海森堡?

标签 c++ pdcurses

所以我正在制作一个带有传送和普通鼠标的贪吃蛇游戏。我有一个像这样运行的循环:

while(snake.alive() && miceEaten < micePerLevel)
{
    displayInfo(lives, score, level, micePerLevel - miceEaten);
    //some code
    if(miceEaten())
    {
        //update score...
    }
    //more stuff...
}

上述代码的问题在于,displayInfo 在分数更新之前被调用,因此在吃掉鼠标后,用户必须等到循环再次运行才能看到他的分数更新。所以我将那一行代码移到了函数的底部:

while(snake.alive() && miceEaten < micePerLevel)
{
    //some code
    if(miceEaten())
    {
        //update score...
    }
    //more stuff...
    displayInfo(lives, score, level, micePerLevel - miceEaten);
}

传送停止工作!每当蛇到达传送点时,程序就会崩溃。 displayInfo 使用以下代码:

stringstream s;
s << "LEVEL " << left << setw(12) << level << "LIVES: " << setw(12) << lives << "MICE LEFT: " << setw(12) << miceLeft
    << "SCORE: " << setw(13) << score;
printLine(0, s.str(), WHITEONBLUE);

其中printLine只有一个color_setmvprintwrefresh()。与传送无关。很奇怪。

所以我去了蛇函数,蛇从传送中获得它的下一个位置:

    body.push_back(teleports[overlap(next)]->teleportFrom(dir)); //next is a Location object

teleports[overlap(next)]->teleportFrom(dir) 返回蛇被传送到的位置。为了弄清崩溃的原因(也许 Teleport 正在返回屏幕外的某个位置?),我在上面的行之前添加了以下 3 行:

    Location l = teleports[overlap(next)]->teleportFrom(dir);
    mvprintw(1, 0, "(%i, %i)", l.x, l.y);
    refresh();

然后问题就消失了!

不仅如此,我还必须拥有这三行。如果我注释掉 mvprintw(1, 0, "(%i, %i)", l.x, l.y);refresh(); 或两者,程序在到达传送点时像以前一样崩溃。

关于可能导致此行为的原因有什么想法吗?

更新:我尝试删除所有警告(主要是关于有符号/无符号数字比较的警告),但到目前为止只剩下 1 个:

warning: reference to local variable 'other' returned

还有代码:

Location& Location::operator = (Location other)
{
    if(this == &other)
        return other;
    x = other.x;
    y = other.y;
    return *this;
}

我该怎么做才能修复此警告?

最佳答案

像这样构建你的赋值运算符:
您应该始终返回 *this(即使它们相等)。但他们永远不会,因为您正在创建本地拷贝(所以这不是您的错误)。

Location& Location::operator = (Location const& other)
{
    // Does it really matter if you assign to self?
    x = other.x;
    y = other.y;
    return *this;
}

对于这样一个简单的类,标准的 copy-and-swap 似乎有点矫枉过正。

附言。您应该修复所有警告(即使它们像无符号不匹配一样简单)。如果你不修复它们,你将对它们的效力免疫并且不会发现真正的问题,因为它被你忽略的警告所包围。所以修复它们(我总是打开使编译器将所有警告视为错误的标志,以便代码在有任何警告时不会编译)。

实现赋值运算符的正确方法(或最普遍接受的好方法)。就是使用copy and swap惯用语:

// notice the parameter is passed by value (i.e. a copy).
// So the copy part is aromatically taken care of here.
// So now you just need tom implement the swap() part of the idiom.
Location& Location::operator = (Location other)
{
    this->swap(other);
    return *this;
}

void Location::swap(Location& other)
{
    std::swap(x, other.x);
    std::swap(y, other.y);
}

关于c++ - 神秘的海森堡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697127/

相关文章:

c++ - C++ 中的双重调度不起作用

c++ - | std::system::recursive_directory_iterator 期间的错误异常 | C++ 中的 Unicode 翻译异常 1113

c++ - 检查 PDCurses/NCurses 中当前/给定位置的字符

c++ - 为什么颜色不适用于 stdscr? (PD诅咒)

c - 如何在 Cmake 中链接 curses.h?

c++ - C++:静态断言参数包与函数类型匹配

c++ - 基于C++11的线程类库

c++ - 为什么模板函数没有出现在 LLVM-IR 中?

c - 为什么 PDcurses 显示的字符串与源文件和流不同?

将 pdcurses 从 arch linux 交叉编译到 windows