This question already has answers here:
Why can templates only be implemented in the header file?

(17个答案)


5个月前关闭。




我用C++创建了一个名为libparse的库,该代码由另一个名为libitcmpmsg.so的库使用。
我一直在尝试在 test.cpp 中测试libitcmpmsg,但是当我尝试构建它时,编译器将返回以下消息:
$libpath/libitcmpmsg.so: reference not found to "void MSG_PARSER::WriteBigEndian<unsigned char>(std::vector<unsigned char, std::allocator<unsigned char> &, unsingned char)"
$libpath/libitcmpmsg.so: reference not found to "unsigned char* MSG_PARSER::ReadBigEndian<unsigned char>(unsigned char&, unsigned char*, unsigned int&)"
$libpath/libitcmpmsg.so: reference not found to "MSG_PARSER::ReadFixedLengthString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, unsigned char*, int, unsigned int&)"
$libpath/libitcmpmsg.so: reference not found to "MSG_PARSER::WriteFixedLengthString(std::vector<unsigned char, std::allocator<unsigned char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)"
libpath只是代表库的路径。 MSG_PARSER是libparser中的 namespace 。
根据错误消息,msg_parser.h中的函数范围为:
    template <class T>
    void WriteBigEndian(std::vector<uint8_t>& target, T source);

    template <class T>
    uint8_t* ReadBigEndian(T &target, uint8_t* source, uint32_t &available);

    uint8_t* ReadFixedLengthString(string& target, uint8_t *source, int size, uint32_t &available);

    void WriteFixedLengthString(std::vector<uint8_t> &target, const string& source, uint32_t size);
似乎libitcmpmsg将错误类型的参数传递给libparser.so。
下面是libitcmpmsg.so使用libparer.so的代码片段
#include "ptcinteraction.h"
#include "msg_parser.h"
#include <stdint.h>

using namespace PTC_INTERACTION;
using namespace MSG_PARSER;


PtcInteraction::PtcInteraction( std::vector<uint8_t> &data )
{
    m_msgBuffer.clear();
    uint32_t tavailable = static_cast<uint32_t>(data.size());
    uint8_t *tsource = &data[0];
    uint8_t value = 0;

    tsource = ReadFixedLengthString(m_message.railScac, tsource,  SCAC_SIZE, tavailable);
    tsource = ReadBigEndian<uint8_t>(m_message.sizeOfPromptTxt, tsource, tavailable );
    tsource = ReadFixedLengthString(m_message.promptTxt, tsource,  m_message.sizeOfPromptTxt, tavailable);
    tsource = ReadBigEndian<uint8_t>(m_message.sizeOfKeyPressedTxt, tsource, tavailable );
    tsource = ReadFixedLengthString(m_message.keyPressedTxt, tsource,  m_message.sizeOfKeyPressedTxt, tavailable);

    if((&data[0] + data.size()) == tsource)
    {
        m_msgBuffer = data;
    }
    else
    {
        m_msgBuffer = {};
    }
}

PtcInteraction::PtcInteraction( OCCPTCMSG::PtcInteractionT &ptcInteraction)
{
    m_msgBuffer.clear();
    m_message = ptcInteraction;

    WriteFixedLengthString(m_msgBuffer, ptcInteraction.railScac, SCAC_SIZE);
    WriteBigEndian<uint8_t>(m_msgBuffer, ptcInteraction.sizeOfPromptTxt );
    .
    .
    .
PTCInteraction是libitcmpmsg.so中的一类,而PtcInteractionT是也由libitcmpmsg定义的结构。
测试代码如下所示:
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include "ptcinteraction.h"
#include "occptcmessages_generated.h"
#include "msg_parser.h"
#include <cstdio>

using namespace PTC_INTERACTION;

int main(void)
{
    OCCPTCMSG::PtcInteractionT teste;


    teste.railScac = "abcd";
    teste.promptTxt = "message test";
    teste.sizeOfPromptTxt = teste.promptTxt.size();
    teste.keyPressedTxt = "test";
    teste.sizeOfKeyPressedTxt = teste.keyPressedTxt.size();

    PTC_INTERACTION::PtcInteraction ptcInter(teste);

    PTC_INTERACTION::PtcInteraction ptcInter2(ptcInter.m_msgBuffer);

    if ( (ptcInter.m_message.railScac == ptcInter2.m_message.railScac) &&
       (ptcInter.m_message.promptTxt == ptcInter2.m_message.promptTxt) &&
       (ptcInter.m_message.sizeOfPromptTxt == ptcInter2.m_message.sizeOfPromptTxt) &&
       (ptcInter.m_message.keyPressedTxt == ptcInter2.m_message.keyPressedTxt) &&
       (ptcInter.m_message.sizeOfKeyPressedTxt == ptcInter2.m_message.sizeOfKeyPressedTxt) )
    {
        std::cout << "Serialization and deserialization succeeded" << std::endl;
    }

}
在代码开发中使用了3个CMakeList:
  • libparser构建器;
  • libitcmpmsg生成器;
  • 测试构建器。

  • 有谁知道为什么编译器会返回问题开头所述的4条错误消息?
    我该如何解决这些问题?
    让我知道您是否需要CMakeLists代码以更好地理解问题。

    最佳答案

    您必须在cpp中实例化专用模板类,或者将模板类的主体放在 header 中:https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

    09-07 03:41