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

发布于 2024-07-11 03:37:02 字数 5509 浏览 8 评论 0原文

好吧,我刚刚完成了最后一个编译器错误(我是这么想的),然后出现了这些错误:

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);

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

Alright, so I just finished my last compiler error (so I thought) and these errors came up:

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

Alright, here's a brief overview.

PI is a static constant in WeaponsDB and is referenced by other classes using WeaponsDB::PI and the appropriate #include (what's wrong with this?)

Most other errors stem from static variables and static methods for timers from the allegro gaming library.

What causes these errors and how might I get rid of them?

Thanks in advance

----------------Edits-------------------

As requested, where the WeaponsDB::PI is declared and defined.
It is declared in WeaponsDB.h:

public:
    static double PI;

But it is defined in another class Driver.cpp:

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

If this is one of the problems with my code I would love to know why this causes an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

久光 2024-07-18 03:37:02

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

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

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

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

Most often, when the linker is failing to detect a static member it is because you to forgot to really define it somewhere, as it was pointed before:

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

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

But in your case, as you are not only missing static variables but also all the rest of the members my bet is that you are not linking the implementation file(s) (.cpp) in your project. You must provide a compilation unit that defines the symbols that were declared in the header, and make the environment compile and link it together. If the symbols belong to a compilation unit in an external library (from your current project) then you must remember to link with the library.

゛时过境迁 2024-07-18 03:37:02

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

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

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

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

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

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

编辑:根据 WP 的评论,在括号中添加了更精确的术语:定义、初始化。

This

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

assigns a value to PI. It does not create space for it (does not define it).

This creates space for (defines) PI and assigns a value to (initializes) it.

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

You should probably also mark PI as "static const" and not just "static". static makes it owned by the class instead of by the instance. const makes it immutable (and enables various optimizations).

You might also consider using M_PI from math.h instead of recreating the constant.

Edit: parenthetically added the more precise terms: defines, initializes due to WP's comment.

残花月 2024-07-18 03:37:02

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

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

希望这可以帮助!

Declaring a variable in one place and assigning it a value in another place shouldn't cause any errors. Make sure you include WeaponsDB.h at the top of all files that reference objects defined there.

Ex: at the top of Driver.cpp, make sure "#include WeaponsDB.h" appears.

Hope this helps!

傾旎 2024-07-18 03:37:02

从阅读其他人的答案中的评论来看,您似乎正在为函数内部分配一个值。 您需要:

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

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

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

From reading your comments in other people's answers, it looks like you are assigning it a value inside of a function. You need to have:

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

.. in a global scope (meaning not inside of a function) of your implementation file, presumably WeaponsDB.cpp. And in that case, "const"ing it would be appropriate. In fact, you can skip the part about putting it in your .cpp and just do it directly in WeaponsDB.h:

public:
    static const double PI = 4*atan(1.0);
我是男神闪亮亮 2024-07-18 03:37:02

缺少大量静态成员。 难道您将静态成员定义集中在一个文件中,然后不链接该文件?

An awful lot of static members are missing. Could it be that you concentrate your static members definitions in one file, and then you don't link that file in?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文