我试图找出我项目中遇到的问题,并将其简化为生成C2664错误的一小段代码。我不理解错误信息,任何人都可以帮助我理解吗?我已经用Google搜索了,我已经浏览了2本C ++书籍,这些代码正是它们中列出的内容,但它对我不起作用。
感谢。
#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;
}
这是错误:
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 ==========
答案 0 :(得分:1)
make_unique的参数是T构造函数的参数,而不是指向T实例的指针,如果已经有一个指向T的指针,则使用常规的unique_ptr ctor。