'No suitable user-defined conversion with inherited classes
I'm struggling with casing a subclass as a superclass to store in a vector of superclass pointers. The objective is to have a vector of ContainerObjects which can be both bins and boxes.
This is a barebones example - in my code, I am passing in a definition of stuff to store in the box and then add the box to my inventory. That part is working fine, but it's adding the box to the inventory which is defeating me.
#include <memory>
#include <vector>
struct ItemData
{
};
class ContainerObject
{
};
using U_ContainerObjectPtr = std::unique_ptr<ContainerObject>;
class Box : ContainerObject
{
public:
Box(ItemData data)
{
// store the data in the box
}
};
// Another container type
class Bin : ContainerObject
{
};
using U_BoxPtr = std::unique_ptr<Box>;
using U_BinPtr = std::unique_ptr<Bin>;
class StorageArea
{
std::vector<U_ContainerObjectPtr> myStorageArea;
public:
void addContainer(U_ContainerObjectPtr container)
{
myStorageArea.push_back(container);
}
};
class DoingWork
{
private:
StorageArea area;
public:
void boxItem(ItemData data)
{
U_BoxPtr newBox = std::make_unique<Box>(data);
area.addContainer(newBox);
}
};
The error I am getting is "No suitable user-defined conversion from U_BoxPtr to U_ContainerObjectPtr" on the call to addContainer() in boxItem() which is understandable. I'm not sure what I need to do to resolve this, or even if this is actually possible.
Reasonably new to C++ as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
