mercredi 5 septembre 2018

Why doesn't make_unique work with a class which has an inline constructor?

I have a well defined class like this,

class A
    {
    public:
        A();
        A(int a);
        A(const A& o);
        A(A&& o);
        ~A();
        A& operator=(const A& o);
        A& operator=(A&& o);
    private:
        int a = 0;
    };

At first, all the methods are in the .h file. Then I used the VS's refactoring function and moved all these methods to the .cpp file (It saved me a lot of labor.) enter image description here

All the auto moved methods have a inline key word before them,

inline A::A()
    {
        cout << "A constructor\n";
    }

I know inline keyword, so I think it's OK.

Then I used this class in the third source file (.cpp file).

void test()
    {
        std::unique_ptr< A> a = std::make_unique<A>();

    }

When I compiled the project, It gave me an error LNK2019 happened in make_unique.

Then I deleted all the inline keyword, it worked. I don't know why make_unique doesn't work when the class's consturctor is inline.

Aucun commentaire:

Enregistrer un commentaire