1

I was happily coding, thinking that everything was alright, until..

libtool: link: g++ -g -O2 -o bbcp bbcp-main.o bbcp-server.o bbcp-client.o bbcp-client_manager.o bbcp-bbcp.pb.o -pthread  -lconfig++ /usr/lib/libprotobuf.so -lz -lpthread -lglog -L/usr/lib -lboost_thread-mt -lboost_system -pthread
bbcp-client_manager.o: In function `BBCP::Server::ClientManager::send_message(boost::shared_ptr<BBCP::Server::Client>, BBCP::Protocol::Message const&)':
/home/eax/BBCP/src/client_manager.cpp:63: undefined reference to `void BBCP::Server::Client::sendPacket<BBCP::Protocol::Message>(BBCP::Protocol::PacketType, BBCP::Protocol::Message const&)'
collect2: ld returned 1 exit status

This not so happy error made me clueless...

client_manager.cpp:

void BBCP::Server::ClientManager::send_message(BBCP::Server::client_ptr client, BBCP::Protocol::Message const &message) {
    google::protobuf::RepeatedPtrField<BBCP::Protocol::Destination> destinations = message.destination();
    BBCP::Protocol::Destination *tmp;
    BBCP::Protocol::Message msg = message;
    std::string tmp_nickname;
    client_ptr tmp_target;
    int x;

    if (!msg.has_sender() || msg.destination_size() == 0) {
        return;
    }

    for (x = 0; x < msg.destination_size(); ++x) {
        tmp_nickname = (destinations.Get(x)).nickname();

        if ((tmp_target = get_nickname(tmp_nickname))) {
            msg.clear_destination();
            tmp = msg.add_destination();
            tmp->set_nickname(tmp_nickname);

            tmp_target->sendPacket<BBCP::Protocol::Message>(BBCP::Protocol::SINGLE_MESSAGE, msg); // this is the line of the error
        }
        else {
            client->sendError(BBCP::Protocol::DESTINATION_UNKNOWN, tmp_nickname);
        }
    }

    return;
}

client_ptr equals to boost::shared_ptr<BBCP::Server::Client>.

Then we have client.cpp:

template<class T>
void BBCP::Server::Client::sendPacket(enum BBCP::Protocol::PacketType type, T const &packet) {
    std::vector<unsigned char> pbuffer;
    BBCP::Protocol::Header header;

    header.set_type(type);
    header.set_length(packet.ByteSize());
    pbuffer.resize(BBCP_HDR_MSG_SIZE + packet.ByteSize());

    if (!header.SerializeToArray(&pbuffer[0], BBCP_HDR_MSG_SIZE)) {
        LOG(ERROR) << "Header of type " << type << " and length " << packet.ByteSize() << " failed to serialize to array. Send aborted.";
    }
    else if (!packet.SerializeToArray(&pbuffer[BBCP_HDR_MSG_SIZE], packet.ByteSize())) {
        LOG(ERROR) << "Packet of type " << type << " and length " << packet.ByteSize() << " failed to serialize to array. Send aborted.";
    }
    else {
        boost::asio::async_write(*socket, boost::asio::buffer(&pbuffer[0], pbuffer.size()), boost::bind(&BBCP::Server::Client::sendPacketWriteHandler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
    }

   return;
}

This function is defined the same way in BBCP::Server::Client as a public member.

As always, any help will be appreciated.

Julian.

3
  • @AndrejsCainikovs how about helping, then? Commented Jan 28, 2012 at 2:01
  • 1
    @Pubby what do you mean? the template is declared in the client header file. Commented Jan 28, 2012 at 2:02
  • @JulianBayardoSpadafora: Use namespaces or redesign in more readable manner. Commented Jan 28, 2012 at 2:04

1 Answer 1

2

Definition of [member] function templates in C++ sources is OK as long as you explicitly instantiate the template in the corresponding source file. Just having the member function template defined in a source and used in a different source won't work. You can try adding something like this after the the definition of the member function template:

template void BBCP::Server::Client::sendPacket<BBCP::Protocol::Message>(
    BBCP::Protocol::PacketType, BBCP::Protocol::Message const &packet);

Of course, this assume that BBCP::Protocol::Message is defined at this point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.