Problem description
I am trying to create a class with a static template function in order to call it from other classes. Inside the class I have a static char array buffer and a static decodeEntity function that takes 3 parameters:
An entity which I want to fill
A function that i want to use to fill the entity
A string content which I want to copy to the buffer
I want to keep the buffer till the end of the program. I don't want to lose it the moment I leave the function decodeEntity.
I wanted to use a simple class where I instantiate an object but it didn't look pretty.
What I tried to solve the problem
I tried the followings:
I moved the Template function inside a cpp body. From what I understood since it's a template function it should remain in the header. When I did that it caused multiple definition compilation error.
I only moved the static member inside a cpp body to initialize it and it didn't work either.
char EntityDecode::buffer[2048] = {};
- I placed the same initialization under the class and it didn't work
Source code
class EntityDecoder {
public:
EntityDecoder() = delete;
EntityDecoder(const EntityDecoder &) = delete;
EntityDecoder &operator=(const EntityDecoder &) = delete;
~EntityDecoder() {}
template <typename Entity, typename DecodeFunction>
static void decodeEntity(Entity &oEntity, DecodeFunction &decodeFunction, const std::string &iRawContent) {
size_t length = iRawContent.size();
for (size_t condentIdx = 0; condentIdx < iRawContent.size(); condentIdx++) {
buffer[condentIdx] = iRawContent[condentIdx];
}
Context content{buffer, iRawContent.size()};
decodeFunction(oEntity, content);
}
private:
static char buffer[2048];
};
Compilation Error
69%] Linking CXX shared library libTsmToLfsResponseEncoderd.so
[ 92%] Built target TsmToLfsResponseEncoder
[100%] Linking CXX executable LFsToTsm-externalEntity-test
../.../libTsmToLfsResponseEncoderd.so: undefined reference to `converter::tsmToLfsResponseEncoder::EntityDecoder::buffer'
}