问题
在我正在创建的 2D 游戏中,我在game
类中有一些向量列表,其中包含指向游戏对象的指针。这些是这样定义的:
std::vector<std::unique_ptr<Enemy>> enemies;
std::vector<std::unique_ptr<Item>> items;
我使用以下代码遍历它们:
for (std::vector<std::unique_ptr<Enemy>>::iterator it = enemies.begin(); it != enemies.end(); ++it) {
(*it)->Draw(screen);
for (std::vector<std::unique_ptr<Item>>::iterator it = items.begin(); it != items.end(); ++it) {
(*it)->Draw(screen);
起初这很好,但是,在某个时候,游戏不再运行,并在尝试开始调试时给了我“无法启动程序”错误。我在网上搜索了解决方案,但找不到任何对我有用的东西。
我将这些向量放在这个答案中的原因是,当循环被删除时,游戏就会运行。我什至发现只有删除第二个循环 ( items
) 才能使游戏运行。然后我将items
数组更改为标准指针向量std::vector<Item*>
,新循环将起作用。使用以下代码循环遍历它的工作方式如下:
for (size_t i = 0; i < items.size(); i++) {
items[i]->Draw(screen);
然后我将代码改回使用unique_ptr
,并删除了填充向量的代码。现在游戏也会运行,所以我担心我在这里做错了什么。这就是我填充向量的方式:
std::unique_ptr<Item> sword(new Item(118 * tilesize, 81 * tilesize, "Sword"));
items.push_back(std::move(sword));
这又很奇怪,因为enemies
向量以相同的方式填充。
谁能指出我做错了什么,以及为什么 Visual Studio 正在构建项目,但由于没有 .exe 而无法运行它?
可能相关的代码
物品类别
class Item : public GameObject
{
public:
Item() {}
Item(int, int, char*);
virtual ~Item(void) {}
void Draw(Surface*);
}; // Item class
Item::Item(int x, int y, char *itemname) :
GameObject(x, y) {
tag = "Item";
name = itemname;
if (name == "Sword") {
sprite = spr_item_sword;
} else if (name == "Coconut") {
}
}
void Item::Draw(Surface *dest) {
GameObject::Draw(dest);
}
游戏对象类
class GameObject
{
public:
Vec2D position;
Sprite *sprite;
char *tag, *name;
GameObject(void) {}
GameObject(int, int);
virtual ~GameObject(void) {}
virtual void Draw(Surface*);
}; // GameObject class
日志
1>------ Build started: Project: Zelda, Configuration: Debug Win32 ------
1>Build started 25-10-2013 16:04:26.
1>InitializeBuildStatus:
1> Creating "Debug\Zelda.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> game.cpp
1>c:\program files\microsoft visual studio 10.0\vc\include\vector(753): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
1> c:\program files\microsoft visual studio 10.0\vc\include\vector(746) : while compiling class template member function 'void std::vector<_Ty>::reserve(unsigned int)'
1> with
1> [
1> _Ty=Tmpl8::TileGraph
1> ]
1> MY_PATH_TO_FILE\game.h(40) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=Tmpl8::TileGraph
1> ]
1> item.cpp
1> template.cpp
1> Generating Code...
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>Link:
1> LINK : MY_PATH_TO_FILE\Debug\Zelda.exe not found or not built by the last incremental link; performing full link
1> Creating library MY_PATH_TO_FILE\Debug\Zelda.lib and object MY_PATH_TO_FILE\Tile Cutter\Debug\Zelda.exp
1>MSVCRTD.lib(cinitexe.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
1>Manifest:
1> All outputs are up-to-date.
1>LinkEmbedManifest:
1> All outputs are up-to-date.
1> Tile_Cutter.vcxproj -> MY_PATH_TO_FILE\Debug\Zelda.exe
1>FinalizeBuildStatus:
1> Deleting file "Debug\Zelda.unsuccessfulbuild".
1> Touching "Debug\Zelda.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:03.10
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========