Error C2664 when trying to create unique_ptr

2.3k Views Asked by At

I'm trying to figure out a problem I have in my project, and I have simplified it down to this little bit of code that generates the C2664 error. I don't understand the error message, could anyone help me to understand? I've googled, and I've looked through 2 C++ books and this code is exactly what is listed in them, but it does not work for me.

Thanks.

#include <memory>

struct A
{
    int b;
};

int main(int argc, char ** argv)
{
    A a;

    std::unique_ptr<A> a_ptr = std::make_unique<A>(new A);

    return 0;
}

And here is the error:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2585): error C2664: 'A::A(const A &)': cannot convert argument 1 from 'A *' to 'A &&'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2584): note: Reason: cannot convert from 'A *' to 'A'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2584): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\users\aksel\documents\visual studio 2017\projects\project1\project1\main.cpp(21): note: see reference to function template instantiation 'std::unique_ptr<A,std::default_delete<_Ty>> std::make_unique<A,A*,0>(A *&&)' being compiled
1>        with
1>        [
1>            _Ty=A
1>        ]
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1

There are 1 best solutions below

0
On

The argument to make_unique is the argument to a T constructor, not a pointer to a T instance, just use the regular unique_ptr ctor if you already have a pointer to a T.