2012-01-28 32 views
1

我愉快地編碼,以爲一切都是正常的,直到..還有一個未定義的引用錯誤

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 

這不是那麼高興的錯誤讓我無言以對......

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等於boost::shared_ptr<BBCP::Server::Client>

然後我們有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; 
} 

此功能在BBCP::Server::Client定義相同的方式作爲一個公共成員。

一如既往,任何幫助將不勝感激。

朱利安。

+0

cpp文件中的模板? – Pubby 2012-01-28 01:54:03

+0

我的眼睛在流血。 – 2012-01-28 01:54:41

+0

@AndrejsCainikovs怎麼樣幫忙呢? – 2012-01-28 02:01:33

回答

2

只要您在相應的源文件中明確實例化模板,C++源代碼中[成員]函數模板的定義就可以了。僅在源中定義成員函數模板並在不同的源中使用它將不起作用。您可以嘗試在成員函數模板的定義後加入這樣的事情:

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

當然,這個假設BBCP::Protocol::Message在此點確定。