I am learning smart pointers, with the following example test.cpp
#include<iostream>
#include<vector>
#include<memory>
struct abstractShape
{
virtual void Print() const=0;
};
struct Square: public abstractShape
{
void Print() const override{
std::cout<<"Square\n";
}
};
int main(){
std::vector<std::unique_ptr<abstractShape>> shapes;
shapes.push_back(new Square);
return 0;
}
The above code has a compilation error "c++ -std=c++11 test.cpp":
smart_pointers_2.cpp:19:12: error: no matching member function for call to 'push_back'
shapes.push_back(new Square);
Could someone help explain the error to me? By the way, when I change push_back
to emplace_back
, the compiler only gives a warning.
emplace_back
? I also triedshapes.emplace_back(std::make_unique<Square>());
, and used c++14 – Steffie