0

I get linker errors when building my program. The problem seems to be my NetworkManager singleton gets pulled into Networkable with the friend statement. I read that this can happen when having the instance declaration in the .hpp file, but if the instance() function isn't being inlined in some way this shouldn't be my case.

I found similar questions here, but no one that answers my problem. Any suggestions?

// Networkable.hpp
#pragma once
class Networkable
{
    friend class NetworkManager;

public:
    ....

//NetworkManager.hpp
#pragma once

class Networkable;

class NetworkManager
{
public:
    static NetworkManager &instance();
    ~NetworkManager();

    void registerNetworkable(Networkable *networkable);
    void unregisterNetworkable(Networkable *networkable);

    void update();
    ...

// errors:
1>NetworkManager.obj : error LNK2005: "public: __thiscall NetworkManager::~NetworkManager(void)" (??1NetworkManager@@QAE@XZ) already defined in Networkable.obj
1>NetworkManager.obj : error LNK2005: "public: static class NetworkManager & __cdecl NetworkManager::instance(void)" (?instance@NetworkManager@@SAAAV1@XZ) already defined in Networkable.obj
1>NetworkManager.obj : error LNK2005: "public: void __thiscall NetworkManager::registerNetworkable(class Networkable *)" (?registerNetworkable@NetworkManager@@QAEXPAVNetworkable@@@Z) already defined in Networkable.obj
1>NetworkManager.obj : error LNK2005: "public: void __thiscall NetworkManager::unregisterNetworkable(class Networkable *)" (?unregisterNetworkable@NetworkManager@@QAEXPAVNetworkable@@@Z) already defined in Networkable.obj
1>NetworkManager.obj : error LNK2005: "public: void __thiscall NetworkManager::update(void)" (?update@NetworkManager@@QAEXXZ) already defined in Networkable.obj
Z317
  • 1

1 Answers1

0

Right, I'm ashamed to say I found the answer. I had included NetworkManager.cpp instead of .hpp in Networkable.cpp. Guess that's what happen when late-night coding...

But it does somewhat raise a valid and easy to make typo - writing .cpp instead of .hpp and not "seeing" the problem. It's much easier to spot a typo with just .h, and makes me question the .hpp file naming standard...

Z317
  • 1