Search code examples
c++visual-studio-2010templatesboost-asioshared-ptr

unresolved external symbol with template implementation on VS2010


i just built a template implementation of my boost network.

here is my template class who is calling my network class :

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();
}
};

and this is my network class who is working with a 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 &);
};

i don't give all of function's code because it's too long and i'm sure it works.

When i build this project i have this following error under VS2010 ultimate :

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 &)

it's long, but i want to give you all of these error because i think the problem come to my template type. so i think my problem come to AbstractNetwork.hpp but i can't find this.

Did you have any idea about that ?


Solution

  • Fix your class. You don't need to repeat the template for each class member function, they're part of the template class already.

    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();
    }
    
    };
    

    Apart from the apparent typo in close, you also don't need to use this everywhere, its redundant and makes the code look messier.