在VS2010上使用模板实现的未解析的外部符号

时间:2011-11-30 18:03:39

标签: c++ visual-studio-2010 templates boost-asio shared-ptr

我刚刚建立了我的助推网络的模板实现。

这是我的模板类,它正在调用我的网络类:

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,但我找不到这个。

你对此有什么想法吗?

1 个答案:

答案 0 :(得分: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,它是多余的,并使代码看起来更麻烦。