【发布时间】:2013-06-22 19:29:12
【问题描述】:
我正在使用 SDL 开发 RTS 游戏。我有一个木场类,其对象将从附近的树木中收集木材。在类中,我创建了一个名为 temp_trees 的向量,并作为构造函数的参数,我使用了一个传入的树对象向量。
木场构造函数:
woodyard::woodyard(int x, int y, int HP, int id, vector<Tree> trees)
{
...
vector<Tree> temp_trees;
for(int i = 0; i < trees.size(); i++)
{
if((trees[i].xPos - 100) / 50 >= x - 5 && (trees[i].xPos - 100) / 50 <= x + 4)
{
if((trees[i].yPos - 100) / 50 >= y - 5 && (trees[i].yPos - 100) / 50 <= y + 4)
{
temp_trees.push_back(trees[i]);
}
}
}
collect_control = 0;
no = 0;
}
collect_wood 函数:
void woodyard::collect_wood(){
if(no == 5)
{
temp_trees[collect_control].drewno -= 1;
if(temp_trees[collect_control].drewno <= 0){
collect_control++;
temp_trees.erase(temp_trees.begin());
}}
no++;
if(no >= 10){
no = 0;
}}
程序在启动后立即崩溃。 任何人都可以看到这段代码中的任何错误吗??
PS:我想在构造函数中将元素从一个向量复制到另一个向量可能有问题。
【问题讨论】:
-
你是通过调试器运行的吗?调试器在哪里停止?它给了你什么错误?
-
它停在“temp_trees[collect_control].drewno -= 1;”
-
temp_trees声明在哪里,WRTcollect_wood方法? -
temp_tree 在woodyard 类的构造函数中(在post 中) collect wood 在woodyard 的头文件中声明
-
当它停在该行时:a)collect_control 的值是多少,b)temp_trees 的大小是多少?