I am getting an unresolved external symbol error (LNK2019, which seems timely given the current year) on the destructor and a static "factory" method that returns a unique_ptr of a class (see code below).
#ifndef ABC_HPP
#define ABC_HPP
#include <memory>
enum SomeEnum { TYPE_A, TYPE_B };
template <SomeEnum Type>
class ABC
{
//------------------------------------------------------------------------------ Constructor / Destructor
private:
ABC();
/* Inaccessible Methods */
ABC(ABC&&) = delete;
ABC(const ABC&) = delete;
ABC& operator=(ABC&&) = delete;
ABC& operator=(const ABC&) = delete;
public:
~ABC();
//------------------------------------------------------------------------------ Factory Method
public:
static std::unique_ptr<ABC<Type>> Create();
//------------------------------------------------------------------------------
};
#endif /* ABC_HPP */
Here is the implementation of the above class:
#include "ABC.hpp"
//------------------------------------------------------------------------------ Constructor(s)
template <SomeEnum Type>
ABC<Type>::ABC() {}
//------------------------------------------------------------------------------ Destructor
template <SomeEnum Type>
ABC<Type>::~ABC() {}
//------------------------------------------------------------------------------ Create
template <SomeEnum Type>
std::unique_ptr<ABC<Type>> ABC<Type>::Create()
{
return std::make_unique<ABC<Type>>();
}
//------------------------------------------------------------------------------
Everything compiles nice and fine... until I do this:
#include "ABC.hpp"
int main(int argc, char* argv[])
{
auto thingA = ABC<TYPE_A>::Create();
return 0;
}
I originally tried to compile it on Visual Studio, but I also tried clang and got the same error.
What exactly am I doing wrong here? I feel like I've run into this error before but that I'm just too dumb to remember what the problem was.
I can get rid of the destructor error if I use raw pointers, but I want to use unique_ptr here. I can also get rid of it by simply commenting out the destructor entirely, but that's not cool with me either. Another way is to have the destructor defined within the header, but that's silly; I should be able to separate the implementation.
So far I have not found a way to get the Create method to work. Am I just trying to do too much here? Do I actually have to make a factory class? Am I too much of a dreamer? Is this question too long???
Thanks in advance to anyone who responds. :)