我不明白我的代码发生了什么:为什么我收到“没有匹配的函数调用 push_back”错误。我只能猜测这是一个新的错误......
int main(){
typedef std::tr1::shared_ptr<Base> Base_p;
typedef std::vector<Base_p> VectorPointers_t;
std::tr1::shared_ptr<Derived> myDer01(Derived);
VectorPointers_t myVector = VectorPointers_t();
myVector.push_back(myDer01);
}
最佳答案
试试这个:
VectorPointers_t myVector;
myVector.push_back(Base_p(new Derived));
您的代码中存在一个大错误:声明 myDer01
的行实际上声明了一个函数 而不是一个变量。但是即使你写的是 (Derived())
而不是 Derived
也是错误的,因为你不能从一个对象构造一个共享指针——只能从一个指针!所以你真的想说 new Derived
。
关于c++ - 派生类的 Push_back shared_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12788538/