0

I've got this class where I define a generic function as so:

// header.hpp

class RosbagManipulator
{
public:
    .
    .
    .

    template<class MessageT>
    std::vector<MessageT> readAllMessagesOfTopic(
        const std::string& topic
        , const std::string& filename)
    {
        Do something...

        return extracted_messages;
    }

    .
    .
    .
};

I use it like:

// demo.cpp
#include "header.hpp"
.
.
.

RosbagManipulator rbman;
auto extracted_messages = rbman.readAllMessagesOfTopic
    <autoware_auto_perception_msgs::msg::PredictedObjects>(
        "/v2x/cpm/objects", "rosbag_autoware_receiver_0.db3");

.
.
.

It works alright when it's in a single header file. But when I try to seperate the declaration and implementation as:

// header.hpp

class RosbagManipulator
{
public:
    .
    .
    .

    template<class MessageT>
    std::vector<MessageT> readAllMessagesOfTopic
        (const std::string& topic
        , const std::string& filename);
    
    .
    .
    .
};
// implementation.cpp
#include "header.hpp"
.
.
.

template<class MessageT>
std::vector<MessageT> RosbagManipulator::readAllMessagesOfTopic(
    const std::string& topic
    , const std::string& filename)
{
    Do something...

    return extracted_messages;
}

.
.
.

With the same usage, I get this error message:

/usr/bin/ld: CMakeFiles/demo.dir/src/demo.cpp.o: in function `main':
demo.cpp:(.text+0x202): undefined reference to `std::vector<autoware_auto_perception_msgs::msg::PredictedObjects_<std::allocator<void> >, std::allocator<autoware_auto_perception_msgs::msg::PredictedObjects_<std::allocator<void> > > > avnv_rosbag::RosbagManipulator::readAllMessagesOfTopic<autoware_auto_perception_msgs::msg::PredictedObjects_<std::allocator<void> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'

I'm new to C++ but I assume it's a linker error complaining it can't find the definition of the function, maybe because I messed up the syntax or something. I don't even know if I can separate the implementation in this case. Any help is greatly appreciated.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Amir Int
  • 45
  • 7

0 Answers0