First of all, there are bunch of similar posts which appear like they are exactly the same issue, but I find that they are different from my problem. I am trying to use smart pointers for the first time in my C++ life. I have trouble making polymorphism with smart pointers. To be precise I am trying to convert the following raw pointers (line # 15 of code shown below) to smart pointers:
class Maze {
public:
Maze() = default;
~Maze() = default;
void BuildStack(MobileRobot *&robot_in_maze);
};
class Target {
public:
Target() = default;
~Target() = default;
void GoWheeled();
private:
Maze wheeledMaze;
MobileRobot *wheeledRobotInMaze = new WheeledRobot();
};
class MobileRobot {
public:
MobileRobot() = default;
MobileRobot(std::string RobotName);
~MobileRobot() = default;
std::string name;
};
class WheeledRobot : public MobileRobot {
public:
WheeledRobot(): MobileRobot("Wheeled Robot") {};
~WheeledRobot() = default;
};
class TrackedRobot : public MobileRobot {
public:
TrackedRobot(): MobileRobot("Tracked Robot") {};
~TrackedRobot() = default;
};
void Maze::BuildStack(MobileRobot *&robot_in_maze) {}
void Target::GoWheeled() {
wheeledMaze.BuildStack(wheeledRobotInMaze);
}
When I try to convert line no 15 of code to a shared pointer type shown below :
std::shared_ptr<MobileRobot> wheeledRobotInMaze = std::make_shared<WheeledRobot>();
I get the following error at line no 41 of code:
Non-const lvalue reference to type 'MobileRobot *' cannot bind to a value of unrelated type 'std::shared_ptr<MobileRobot>'
Why is this happening ?