简体   繁体   English

未解决的外部

[英]Unresolved Externals

I am working on a simple top down shooter and wanted to move my ships to a separate ShipManager class, where I can manage all of them from a single location. 我正在研究一个简单的自上而下的射击游戏,并希望将我的船只移到一个单独的ShipManager类,在那里我可以从一个位置管理所有这些。 However, upon starting this I get a linker error on my playerShip: 但是,在启动时,我的playerShip上出现了一个链接器错误:

error LNK2001: unresolved external symbol "public: static class Ship * ShipManager::playerShip" 错误LNK2001:未解析的外部符号“public:static class Ship * ShipManager :: playerShip”

ShipManager.h looks like this: ShipManager.h如下所示:

class Ship;

class ShipManager
{
public:
static Ship*    playerShip;
};

I have nothing in the ShipManager .cpp yet. 我在ShipManager .cpp中什么都没有。 What am I missing? 我错过了什么? The only other place I use this code is in my game class where I am actually calling ShipManager::playerShip, and I don't get any errors there. 我使用此代码的唯一其他地方是在我的游戏类中,我实际上正在调用ShipManager :: playerShip,我没有在那里得到任何错误。

I include "ShipManager.h" in my game.cpp, so it should find it right? 我在我的game.cpp中包含“ShipManager.h”,所以应该找到它吗? I have a feeling I'm forgetting something simple in this class. 我有一种感觉,我在这堂课中忘记了一些简单的事情。

Static members have to be defined somewhere. 必须在某处定义静态成员。 You are declaring playerShip , but not defining it. 您正在声明playerShip ,但没有定义它。 You need to add somewhere, necessarily and only one cpp file : 您需要添加某个地方, 必须只添加一个cpp文件

Ship* ShipManager::playerShip;

You only declared the static member, you also need to define it in (only)one of your cpp files: 您只声明了静态成员,还需要在(仅)一个cpp文件中定义它:

Ship* ShipManager::playerShip;

Good Read: 好读:
What is the difference between a definition and a declaration? 定义和声明之间有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM