C++ C++;使用std::move(nullptr)时,唯一ptr的运算符=出错
我已经看到并更正了我的代码:C++ C++;使用std::move(nullptr)时,唯一ptr的运算符=出错,c++,unique-ptr,C++,Unique Ptr,我已经看到并更正了我的代码: int solutionChooser = m_configFile.getChosenSolution(); ISolution* currentSolution; switch (solutionChooser) { case 1: { currentSolution = new Solution1()); break; } case 2: { currentSolution = new Solution2());
int solutionChooser = m_configFile.getChosenSolution();
ISolution* currentSolution;
switch (solutionChooser)
{
case 1:
{
currentSolution = new Solution1());
break;
}
case 2:
{
currentSolution = new Solution2());
break;
}
case 3:
{
currentSolution = new Solution3());
break;
}
case 4:
{
currentSolution = new Solution4());
break;
}
default:
{
std::cout << "The specified solution does not exists\n";
return;
}
}
现在我得到的错误如下:
error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<ISolution>’ and ‘std::remove_reference<long int>::type {aka long int}’)
错误:“operator=”不匹配(操作数类型为“std::unique_ptr”和“std::remove_reference::type{aka long int}”)
我有ISolution
作为接口,SolutionX
是从ISolution
如何解决这个问题?我做错了什么?
std::unique\u ptr
已删除操作符=
,这就是为什么您不能使用它
要重置std::unique\u ptr
,请使用reset()
方法:
currentSolution.reset(nullptr);
但您不必这样做,因为初始值无论如何都是
nullptr
。std::unique\u ptr
已删除运算符=
,这就是您无法使用它的原因
要重置std::unique\u ptr
,请使用reset()
方法:
currentSolution.reset(nullptr);
但是您不必这样做,因为初始值无论如何都是
nullptr
。您的编译器是错误的,n3376std::unique\u ptr
应该具有以下重载
unique_ptr& operator=(nullptr_t) noexcept;
因此,您的代码应该可以正常工作。您的编译器是错误的,n3376
std::unique\u ptr
应该具有以下重载
unique_ptr& operator=(nullptr_t) noexcept;
因此,您的代码应该可以正常工作。我不能添加注释,所以我将发布我对答案的想法。。 我相信你的问题是复制构造函数。运算符=未使用unique_ptr定义,因此您必须使用移动构造函数。我不记得正确的语法,但它应该类似于:
/* Header file */
std::unique_ptr<ISolution> currentSolution;
/* CPP file */
std::unique_ptr<ISolution> currentSolution2(new ISolution);
currentSolution = std::move(currentSolution2);
/*头文件*/
std::唯一的解决方案;
/*CPP文件*/
标准:唯一的ptr电流解决方案2(新隔离);
currentSolution=std::move(currentSolution2);
这里可能有一些错误,但希望它能让你走上正确的轨道。如果你想要一个有效的例子,我有一个关于地板的例子,用户Simple2012。
链接此处:
查看arr.h和arr.cpp以获得一个具体的示例,但是我在那里使用的是数组而不是类,差别不大。我不能添加注释,所以我只想发布我对答案的想法。。 我相信你的问题是复制构造函数。运算符=未使用unique_ptr定义,因此您必须使用移动构造函数。我不记得正确的语法,但它应该类似于:
/* Header file */
std::unique_ptr<ISolution> currentSolution;
/* CPP file */
std::unique_ptr<ISolution> currentSolution2(new ISolution);
currentSolution = std::move(currentSolution2);
/*头文件*/
std::唯一的解决方案;
/*CPP文件*/
标准:唯一的ptr电流解决方案2(新隔离);
currentSolution=std::move(currentSolution2);
这里可能有一些错误,但希望它能让你走上正确的轨道。如果你想要一个有效的例子,我有一个关于地板的例子,用户Simple2012。
链接此处:
查看arr.h和arr.cpp以获得一个具体的示例,但是我在那里使用的是数组而不是类,差别不大。只需从默认情况下删除该行即可。您似乎不需要它。请尝试使用
currentSolution.reset(nullptr)
;错误是因为nullptr(至少在visualstudio上)可以隐式转换为int,而int不是任何类型的指针。或者尝试执行'std::move(nullptr)move(nullptr)
?嘿,我还是要用那nullptr
!只需从默认情况下删除该行。您似乎不需要它。请尝试使用currentSolution.reset(nullptr)
;错误是因为nullptr(至少在visualstudio上)可以隐式转换为int,而int不是任何类型的指针。或者尝试执行'std::move(nullptr)move(nullptr)
?嘿,我还是要用那nullptr
!我在Ubuntu14.04上使用g++9,我在Ubuntu14.04上使用g++9,看起来就像你说的那样。不工作:move(nullptr)
,move(nullptr)
似乎像你说的那样工作。不工作:move(nullptr)
,move(nullptr)