我从此构造函数中收到此错误
严重性代码描述项目文件行抑制状态错误
(active) E0289 no instance of constructor "std::unique_ptr<_Ty, _Dx>::unique_ptr [with _Ty=InputComponent, _Dx=std::default_delete<InputComponent>]" matches the argument list
代码:
class GameWorldObject
{
public:
GameWorldObject::GameWorldObject(std::unique_ptr<GraphicsComponent> graphicsComponent, std::unique_ptr<LogicComponent> logicComponent, std::unique_ptr<InputComponent> inputComponent)
:
m_graphicsComponent( std::move(graphicsComponent) ), // ERROR
m_inputComponent( std::move(logicComponent) ), // ERROR
m_logicComponent( std::move(inputComponent) ) // ERROR
{}
// these auto initialize to null
std::unique_ptr<GraphicsComponent> m_graphicsComponent;
std::unique_ptr<LogicComponent> m_logicComponent;
std::unique_ptr<InputComponent> m_inputComponent;
};
我很傻,因为此示例对我有用,并且看起来与之前的示例相同:
struct Animal {
virtual ~Animal() {}
virtual void speak() = 0;
};
struct Dog : Animal {
void speak() override { std::cout << "Woof!\n"; }
};
std::unique_ptr<Animal> createPet() {
return std::make_unique<Dog>();
}
class House {
public:
House(std::unique_ptr<Animal> pet) // seems identical
:
m_pet(std::move(pet)) // seems identical
{}
std::unique_ptr<Animal> m_pet; // seems identical
};
void main()
{
auto pet = createPet(6.0);
pet->speak();
House house(std::move(pet));
//pet->speak(); err pet is nullptr
house.m_pet->speak();
}
这两者之间有什么区别? 为什么前一个不起作用?
请注意,我尚未在InputComponent
,GraphicsComponent
或LogicComponent
中实现任何类型的构造函数,析构函数或复制构造函数。
实际上,这是InputComponent
class InputComponent
{
};