- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我正在制作一个带有传送和普通鼠标的贪吃蛇游戏。我有一个像这样运行的循环:
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_set
、mvprintw
和refresh()
。与传送无关。很奇怪。
所以我去了蛇函数,蛇从传送中获得它的下一个位置:
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/
我正在使用 Aviary 图片编辑库开发 Android 应用程序,但遇到了问题。在调试器中运行时,一切运行良好。但是,当我在手机上运行它时,它崩溃了,日志中出现“java.lang.IllegalA
这个问题在这里已经有了答案: Loop doesn't see value changed by other thread without a print statement (1 个回答) 关闭
求助!我真是无计可施了。我的程序是一个小的个人笔记管理器(谷歌搜索“cintanotes”)。在某些计算机上(当然我不拥有它们)它在启动后立即崩溃并出现未处理的异常。这些计算机没有什么特别之处,只是它
我是一名优秀的程序员,十分优秀!