2

好的,所以我刚刚完成了最后一个编译器错误(所以我想)并且出现了这些错误:

1>GameEngine.obj : error LNK2001: unresolved external symbol "public: static double WeaponsDB::PI" (?PI@WeaponsDB@@2NA)
1>Component.obj : error LNK2001: unresolved external symbol "public: static double WeaponsDB::PI" (?PI@WeaponsDB@@2NA)
1>Coordinate.obj : error LNK2019: unresolved external symbol "public: static double WeaponsDB::PI" (?PI@WeaponsDB@@2NA) referenced in function "public: double __thiscall Coordinate::distanceFrom(class Coordinate *)" (?distanceFrom@Coordinate@@QAENPAV1@@Z)
1>Driver.obj : error LNK2001: unresolved external symbol "public: static double WeaponsDB::PI" (?PI@WeaponsDB@@2NA)
1>Environment.obj : error LNK2001: unresolved external symbol "public: static double WeaponsDB::PI" (?PI@WeaponsDB@@2NA)
1>Environment.obj : error LNK2001: unresolved external symbol "public: static bool Environment::spyFlag" (?spyFlag@Environment@@2_NA)
1>Environment.obj : error LNK2001: unresolved external symbol "private: static class Environment * Environment::instance_" (?instance_@Environment@@0PAV1@A)
1>Environment.obj : error LNK2019: unresolved external symbol "public: static void __cdecl Environment::spyAlertOver(void)" (?spyAlertOver@Environment@@SAXXZ) referenced in function "public: void __thiscall Environment::notificationOfSpySuccess(void)" (?notificationOfSpySuccess@Environment@@QAEXXZ)
1>GameDriver.obj : error LNK2019: unresolved external symbol "public: static void __cdecl MainMenu::gameOver(int)" (?gameOver@MainMenu@@SAXH@Z) referenced in function "public: static void __cdecl GameDriver::run(void)" (?run@GameDriver@@SAXXZ)
1>GameDriver.obj : error LNK2019: unresolved external symbol "public: static void __cdecl GameDriver::gatherInput(void)" (?gatherInput@GameDriver@@SAXXZ) referenced in function "public: static void __cdecl GameDriver::run(void)" (?run@GameDriver@@SAXXZ)
1>GameDriver.obj : error LNK2019: unresolved external symbol "public: static void __cdecl GameDriver::ticker(void)" (?ticker@GameDriver@@SAXXZ) referenced in function "public: static void __cdecl GameDriver::run(void)" (?run@GameDriver@@SAXXZ)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static int GameDriver::ticks" (?ticks@GameDriver@@2HA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::evaluatingInputFlag" (?evaluatingInputFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyQuitFlag" (?keyQuitFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyToggleWeaponRightFlag" (?keyToggleWeaponRightFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyToggleWeaponLeftFlag" (?keyToggleWeaponLeftFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyFireFlag" (?keyFireFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyLeftFlag" (?keyLeftFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyRightFlag" (?keyRightFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyUpFlag" (?keyUpFlag@GameDriver@@2_NA)
1>GameDriver.obj : error LNK2001: unresolved external symbol "public: static bool GameDriver::keyDownFlag" (?keyDownFlag@GameDriver@@2_NA)
1>GUI_Env.obj : error LNK2001: unresolved external symbol "private: static struct BITMAP * GUI_Env::buffer" (?buffer@GUI_Env@@0PAUBITMAP@@A)
1>GUI_Info.obj : error LNK2001: unresolved external symbol "private: static struct BITMAP * GUI_Info::buffer" (?buffer@GUI_Info@@0PAUBITMAP@@A)
1>MenuDriver.obj : error LNK2019: unresolved external symbol "public: static void __cdecl MainMenu::displayMenu(void)" (?displayMenu@MainMenu@@SAXXZ) referenced in function "public: static void __cdecl MenuDriver::start(void)" (?start@MenuDriver@@SAXXZ)
1>SpaceObjectFactory.obj : error LNK2001: unresolved external symbol "private: static class SpaceObjectFactory * SpaceObjectFactory::_instance" (?_instance@SpaceObjectFactory@@0PAV1@A)
1>Spy.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall UnFormationable::sameTypeOfSpaceObjectAs(class SpaceObject *)" (?sameTypeOfSpaceObjectAs@UnFormationable@@UAE_NPAVSpaceObject@@@Z) referenced in function "public: virtual bool __thiscall Spy::sameTypeOfSpaceObjectAs(class SpaceObject *)" (?sameTypeOfSpaceObjectAs@Spy@@UAE_NPAVSpaceObject@@@Z)
1>WeaponsDB.obj : error LNK2001: unresolved external symbol "private: static class WeaponsDB * WeaponsDB::_instance" (?_instance@WeaponsDB@@0PAV1@A)
1>C:\Users\Owner\Desktop\Bosconian\code\Bosconian\Debug\Bosconian.exe : fatal error LNK1120: 23 unresolved externals

好的,这是一个简短的概述。

PI 是 WeaponsDB 中的静态常量,其他类使用 Wea​​ponsDB::PI 和适当的#include 引用(这有什么问题?)

大多数其他错误源于 allegro 游戏库中计时器的静态变量和静态方法。

是什么导致了这些错误,我该如何摆脱它们?

提前致谢

----------------编辑-------------------

根据要求,在哪里声明和定义了 WeaponsDB::PI。它在 WeaponsDB.h 中声明:

public:
    static double PI;

但它是在另一个类 Driver.cpp 中定义的:

WeaponsDB::PI = 4*atan(1.0);

如果这是我的代码的问题之一,我很想知道为什么会导致错误。

4

5 回答 5

10

大多数情况下,当链接器无法检测到静态成员时,这是因为您忘记在某处真正定义它,正如之前指出的那样:

// header
class X {
   static const int y;
};

// cpp
const int X::y = 1;

但是在您的情况下,因为您不仅缺少静态变量,而且还缺少所有其他成员,所以我敢打赌,您没有链接项目中的实现文件(.cpp)。您必须提供一个编译单元来定义在标头中声明的符号,并使环境编译并将其链接在一起。如果符号属于外部库(来自当前项目)中的编译单元,那么您必须记住与库链接。

于 2008-12-19T07:15:19.823 回答
2

WeaponsDB::PI = 4*atan(1.0);

为 PI 赋值。它不会为它创造空间(没有定义它)。

这为(定义)PI 创造了空间,并为它分配了一个值(初始化)。

double WeaponsDB::PI = 4*atan(1.0);

您可能还应该将 PI 标记为“静态常量”,而不仅仅是“静态”。static 使其归类而不是实例所有。const 使其不可变(并启用各种优化)。

您也可以考虑使用 math.h 中的 M_PI 而不是重新创建常量。

编辑:括号中添加了更精确的术语:由于 WP 的评论而定义、初始化。

于 2008-12-19T05:34:47.717 回答
0

在一个地方声明一个变量并在另一个地方给它赋值应该不会导致任何错误。确保在引用其中定义的对象的所有文件的顶部包含 WeaponsDB.h。

例如:在 Driver.cpp 的顶部,确保出现“#include WeaponsDB.h”。

希望这可以帮助!

于 2008-12-19T05:35:17.967 回答
0

通过阅读您在其他人的答案中的评论,您似乎正在为它分配一个函数内部的值。你需要有:

double WeaponsDB::PI = 4*atan(1.0);

.. 在你的实现文件的全局范围内(意味着不在函数内部),大概是 WeaponsDB.cpp。在那种情况下,“const”它是合适的。事实上,您可以跳过将其放入 .cpp 的部分,直接在 WeaponsDB.h 中进行:

public:
    static const double PI = 4*atan(1.0);
于 2008-12-19T18:37:44.823 回答
0

大量的静态成员丢失了。是否将静态成员定义集中在一个文件中,然后不链接该文件?

于 2008-12-19T18:41:09.403 回答