0

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:

  1. An entity which I want to fill

  2. A function that i want to use to fill the entity

  3. 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'
}
Community
  • 1
  • 1
gringo
  • 373
  • 2
  • 4
  • 15
  • 1
    Not sure I get the problem, but how about using the good'ol static initialization trick? (a function holding the data as static local variable). – kebs Jan 19 '19 at 16:55
  • @kebs thank you so much for your help. i saw that in an answer. I dont know how to plug it in the class. i'll look for it – gringo Jan 19 '19 at 17:00
  • For the record: @eerorika : I disagree with the closure/dupe, I sincerely think you read this question too quickly (you probably only read the error message). The one you linked to is unrelated. Here the OP needs static init. There's probably a dupe too, but surely not that one! – kebs Jan 19 '19 at 17:09
  • @kebs should I try and give a code that can be used? instead of this one? – gringo Jan 19 '19 at 17:16
  • Not sure I get what you want to do. The question was closed by some guy (see my comment), so nobody can add answers... – kebs Jan 19 '19 at 17:20
  • @kebs the dupe shows how to define a static member. – eerorika Jan 19 '19 at 17:20
  • @kebs i found the solutionnnn u were right thanks a lot for your comment. I wrote this I mean i doesn't fit in here. But it works. I learned something new thanks a lott. typedef char char_array[2048]; static char_array & getInstance() { static char array[2048] = {}; return array; } – gringo Jan 19 '19 at 20:27
  • See @eerorika ? That has nothing to do with defining a static **member**, its about a static **variable**. Next time, please take some time to carefully read question before closing. – kebs Jan 19 '19 at 22:59
  • @kebs `undefined reference to converter::tsmToLfsResponseEncoder::EntityDecoder::buffer'` is all about not defining a static member. Of course, if OP changes their code to use a local static instead of a static member, then they will no longer have a problem with not defining a static member. – eerorika Jan 19 '19 at 23:02
  • That's what I said, you only read the error message, without considering what he was asking for and how to do it. As he explained, he tried different approaches, thus the error message. But the question you linked to **does not** answer his problem, is far too generic. So, nevermind, he got a solution, not thanks to you. – kebs Jan 19 '19 at 23:37

0 Answers0