1

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. :)

  • Question is of reasonable length and contains everything needed to diagnose. A "good" question in my opinion. – user4581301 Jun 06 '19 at 19:35
  • @user4581301 Thanks for the feedback, and that linked help solve the destructor error, but I'm still having a problem with the Create method. This time it's a C2248 error saying "'ABC::ABC': cannot access private member declared in class 'ABC'" and same thing for TYPE_B. And that's being thrown from the "memory" file, so I assume the problem has to be in std::make_unique. I know this is an odd question, but... Is there anyway I can make std::make_unique a friend? – Nicholas Bonjour Jun 06 '19 at 19:49
  • [How to make std::make_unique a friend of my class](https://stackoverflow.com/questions/33905030/how-to-make-stdmake-unique-a-friend-of-my-class) TL;DR: Add `friend std::unique_ptr std::make_unique();` to the `ABC` class definition. – user4581301 Jun 06 '19 at 20:40

0 Answers0