错误LNK2001和错误LNK2019(C ++) - 请求了解这些错误

时间:2008-12-19 05:12:22

标签: c++ linker-errors lnk2019

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

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中的静态常量,并且使用WeaponsDB :: PI以及相应的#include引用其他类(这有什么问题?)

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

导致这些错误的原因以及我如何摆脱它们?

提前致谢

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

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

public:
    static double PI;

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

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

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

5 个答案:

答案 0 :(得分:10)

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

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

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

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

答案 1 :(得分:2)

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

为PI分配值。它不会为它创建空间(不定义它)。

这为(定义)PI创建了空间,并为其赋值(初始化)。

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

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

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

编辑:以括号方式添加更精确的字词:定义,由于WP的评论而初始化。

答案 2 :(得分:0)

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

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

希望这有帮助!

答案 3 :(得分:0)

通过在其他人的答案中阅读您的评论,看起来您在函数内部分配了一个值。你需要:

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

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

public:
    static const double PI = 4*atan(1.0);

答案 4 :(得分:0)

缺少很多静态成员。是否可以将静态成员定义集中在一个文件中,然后不将该文件链接到?