0

I have in MyClass.h file

template<class TemplateClass>
class MyClass{
  public: 
    MyClass(std::string input);
    void funcA();
    void funcB();
    void funcC();

  private:
    std::string _input;
    TemplateClass _myTemplateClass;
}

And in MyClass.cpp file:

template<class TemplateClass>
MyClass<TemplateClass>::MyClass(): _input(input) {}

template<class TemplateClass>
void MyClass<TemplateClass>::funcA(){
  std::cout<<" just prints A, doesn't even refer to the TemplateClass";
}

template<class TemplateClass>
void MyClass<TemplateClass>::funcB(){
  std::cout<<" just prints B, doesn't even refer to the TemplateClass";
}

template<class TemplateClass>
void MyClass<TemplateClass>::funcC(){
  _myTemplateClass.doSomething();
}

In my main.cpp:

#include "MyClass.h"
auto myClass  = MyClass<ConcreteClass>("input"); //This results in a undefined symbol error.

Two questions:

  1. Is it necessary to add template on top of every function (funcA, funcB, funcC) definition in MyClass.cpp, even if they don't refer to TemplateClass?

  2. Why do I get an undefined symbol error in my main.cpp when I try to initialize MyClass?

user1008636
  • 2,989
  • 11
  • 31
  • 45
  • 1
    Your bug is having a cpp file to implement your template. Move your implementation into the header. – drescherjm Oct 25 '22 at 19:14
  • 1
    The answer to question 1 is yes. `MyClass` is a template, so you need the template part when doing out of line function definitions. – NathanOliver Oct 25 '22 at 19:15
  • @drescherjm what does 'move your template into the header' mean? Can you elaborate? – user1008636 Oct 25 '22 at 19:17
  • 1
    Cut all of the code from your `MyClass.cpp` file and paste it at the bottom of the `MyClass.h` file. Then remove `MyClass.cpp` from your project. Templates are different from non-template code. You could also rename the .cpp file to give it a different extension like those discussed in this question: [https://stackoverflow.com/questions/29264656/c-template-implementation-file-extension-convention](https://stackoverflow.com/questions/29264656/c-template-implementation-file-extension-convention) and just include it from your header and remove from your project (if using an IDE with a project) – drescherjm Oct 25 '22 at 19:22
  • @drescherjm got it, this is crazy. C++ has such arbitrary and unnatural rules like this. – user1008636 Oct 25 '22 at 19:26
  • It's not so crazy once you understand what exactly is going on. A class or function template isn't an actual thing like a normal class or function, it is a recipe on how to make a class or function. Because of that the compiler needs the recipe in order to check that the requested version your are asking for can be created. A `Foo` and a `Foo` can be so completely different that they share no code at all. – NathanOliver Oct 25 '22 at 19:32
  • And why does moving the class definitions to header file help resolve that problem? @NathanOliver – user1008636 Oct 25 '22 at 19:33
  • 1
    When all of the code is in the header file, and you include that header file in your file, then the compiler has the full recipe. When you split the code like you did, it only has part of the recipe. Its like have an ingredient list but no preparation/cooking instructions. – NathanOliver Oct 25 '22 at 19:53

0 Answers0