2014-12-21 63 views
0

構建我的程序時出現鏈接錯誤。問題似乎是我的NetworkManager單身人士通過friend語句被拉入Networkable。我讀過在.hpp文件中有實例聲明時會發生這種情況,但如果instance()函數沒有以某種方式內聯,這不應該是我的情況。單身人士引起鏈接錯誤的朋友聲明

我在這裏發現了類似的問題,但沒有人回答我的問題。有什麼建議麼?

// 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)" ([email protected]@[email protected]) already defined in Networkable.obj 
1>NetworkManager.obj : error LNK2005: "public: static class NetworkManager & __cdecl NetworkManager::instance(void)" ([email protected]@@[email protected]) already defined in Networkable.obj 
1>NetworkManager.obj : error LNK2005: "public: void __thiscall NetworkManager::registerNetworkable(class Networkable *)" ([email protected]@@[email protected]@@Z) already defined in Networkable.obj 
1>NetworkManager.obj : error LNK2005: "public: void __thiscall NetworkManager::unregisterNetworkable(class Networkable *)" ([email protected]@@[email protected]@@Z) already defined in Networkable.obj 
1>NetworkManager.obj : error LNK2005: "public: void __thiscall NetworkManager::update(void)" ([email protected]@@QAEXXZ) already defined in Networkable.obj 

回答

0

對,我很慚愧地說我找到了答案。我在Networkable.cpp中包含了NetworkManager.cpp而不是.hpp。 猜猜這是深夜編碼時發生的事情...

但它的確提高了一個有效且易於使錯字 - 寫入.cpp而不是.hpp而不是「看到」問題。使用.h發現錯字更容易,並且使我懷疑.hpp文件的命名標準...