简体   繁体   中英

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. However, upon starting this I get a linker error on my playerShip:

error LNK2001: unresolved external symbol "public: static class Ship * ShipManager::playerShip"

ShipManager.h looks like this:

class Ship;

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

I have nothing in the ShipManager .cpp yet. 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.

I include "ShipManager.h" in my game.cpp, so it should find it right? 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. You need to add somewhere, necessarily and only one cpp file :

Ship* ShipManager::playerShip;

You only declared the static member, you also need to define it in (only)one of your cpp files:

Ship* ShipManager::playerShip;

Good Read:
What is the difference between a definition and a declaration?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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