2011-11-30 52 views
0

我剛剛構建了我的助推網絡的模板實現。無法解析的外部符號與VS2010上的模板實現

這裏是我是誰喊我的網絡類模板類:

AbstractNetwork.hpp : 

#include "Network.hpp" 

template <typename T, typename R, typename S, typename A> 
class  AbstractNetwork 
{ 
private: 
    T  connection; 

public: 
AbstractNetwork(A acceptor) 
{ 
    connection = T::create(acceptor); 
} 

void write(const R msg) 
{ 
    connection->Swrite(msg); 
} 

R read(void) 
{ 
    connection->Sread(); 
    return (connection->getIdata()); 
} 

S getSocket(void) 
{ 
    return (connection->getSocket()); 
} 

void close(void) 
{ 
    connection->close(); 
} 
}; 

,這是我的網絡類誰正在與boost::tcp::socket

Network.hpp 

class  Network : public boost::enable_shared_from_this<Network> 
{ 
private: 
    tcp::socket      socket; 
    std::vector<char>    Idata; 
    char       Iheader[MYINT]; 
    bool       readHeader; 
    void       endRead(const error_code& error, size_t nbytes); 
    void       endWrite(const error_code &error); 
    size_t       Isize; 

public: 
    typedef boost::shared_ptr<Network> ptr; 

    Network(io_service &); 
    ~Network(); 
    void    close(void); 
    void    Sread(void); 
    void    Swrite(Commande *); 
    tcp::socket&  getSocket(void); 
    Commande*   getIdata(void); 
    std::string   convertHeader(void); 
    static ptr   create(io_service &); 
}; 

我不給所有的函數的代碼,因爲它太長,我敢肯定它的工作原理。

當我建立這個項目我已經在VS2010最終此以下錯誤:

error LNK2001: unresolved external symbol "public: void __thiscall AbstractNetwork<class boost::shared_ptr<class Network>,struct Commande *,class boost::asio::basic_stream_socket<class boost::asio::ip::tcp,class boost::asio::stream_socket_service<class boost::asio::ip::tcp> > &,class boost::asio::io_service &>::close(void)" 

error LNK2001: unresolved external symbol "public: void __thiscall AbstractNetwork<class boost::shared_ptr<class Network>,struct Commande *,class boost::asio::basic_stream_socket<class boost::asio::ip::tcp,class boost::asio::stream_socket_service<class boost::asio::ip::tcp> > &,class boost::asio::io_service &>::write(struct Commande * const)" 

error LNK2001: unresolved external symbol "public: class boost::asio::basic_stream_socket<class boost::asio::ip::tcp,class boost::asio::stream_socket_service<class boost::asio::ip::tcp> > & __thiscall AbstractNetwork<class boost::shared_ptr<class Network>,struct Commande *,class boost::asio::basic_stream_socket<class boost::asio::ip::tcp,class boost::asio::stream_socket_service<classboost::asio::ip::tcp> > &,class boost::asio::io_service &>::getSocket(void)" 

error LNK2001: unresolved external symbol "public: __thiscall AbstractNetwork<class boost::shared_ptr<class Network>,struct Commande *,class boost::asio::basic_stream_socket<class boost::asio::ip::tcp,class boost::asio::stream_socket_service<class boost::asio::ip::tcp> > &,class boost::asio::io_service &>::AbstractNetwork<class boost::shared_ptr<class Network>,struct Commande *,classboost::asio::basic_stream_socket<class boost::asio::ip::tcp,class boost::asio::stream_socket_service<class boost::asio::ip::tcp> > &,class boost::asio::io_service &>(class boost::asio::io_service &) 

它的長,但我想給你所有的這些錯誤,因爲我認爲這個問題來到我的模板類型。所以我認爲我的問題來到AbstractNetwork.hpp,但我找不到這個。

你有什麼想法嗎?

+0

您是否使用方法的實現編寫了Network.cpp源代碼文件?你有沒有把它添加到你的項目?鏈接器錯誤說你沒有。 –

+0

是啊是的,我有一個Network.cpp在我的projet,(我沒有給它,因爲它太長了),我敢肯定他是我的項目,因爲我編寫和測試之前添加我的AbstractNetwork –

回答

0

修復你的課。您不需要爲每個類成員函數重複模板,它們已經是模板類的一部分。

template <typename T, typename R, typename S, typename A> 
class  AbstractNetwork 
{ 
private: 
    T  connection; 

public: 
AbstractNetwork(A acceptor) 
{ 
    this->connection = T::create(acceptor); 
} 

void write(const R msg) 
{ 
    this->connection->Swrite(msg); 
} 

read(void) 
{ 
    this->connection->Sread(); 
    return (this->connection->getIdata()); 
} 

getSocket(void) 
{ 
    return (this->connection->getSocket()); 
} 

close(void) 
{ 
    this->close(); // <<--- is this a mistake? 
    //Probably you wanted this->connection->close(); 
} 

}; 

除了在close明顯的錯字,你也不需要使用this無處不在,它的冗餘,使代碼看起來混亂。

+0

是啊是啊這是一個錯誤的遺憾。我正在做這些更正。 –

+0

現在我有這些錯誤:'錯誤C2039:'創建':不是'boost :: shared_ptr '和'錯誤C3861:'創建':標識符未找到'的成員' –

+0

您正在傳遞一個類作爲' T'沒有實現你期望的接口('static T :: create')。修復。我很高興你原來的問題已經解決了。 – littleadv