我从这个构造函数中得到这个错误
严重性代码描述项目文件行抑制状态错误
(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
{
};