0

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.

Community
  • 1
  • 1
Zhang
  • 3,030
  • 2
  • 14
  • 31
  • 2
    `inline` functions have to be defined (not just declared) in every source file they are used. The keyword is meant to be applied to functions defined in a header. It makes no sense to use it for functions defined in a .cpp file. – Igor Tandetnik Sep 06 '18 at 14:34
  • @NikitaKniazev, I have thought it's an unique_ptr problem. I just tested,it's a moving inline function to .cpp file problem. – Zhang Sep 07 '18 at 01:16

1 Answers1

1

I have thought it's an unique_ptr problem. I just tested,it's a moving inline function to .cpp file problem.

With an inline constructor in .cpp file. Just creating a class A instance would fail.

A a;

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: __thiscall __AWellDefinedClass::A::A(void)" (??0A@__AWellDefinedClass@@QAE@XZ) referenced in function "void __cdecl __pointerTest::test(void)" (?test@__pointerTest@@YAXXZ) ForTest D:\test\ForTest\ForTest\pointerTest.obj 1

Zhang
  • 3,030
  • 2
  • 14
  • 31