我需要一个std::vector
of boost::ptr_vector
s。为了使它们的管理更容易,我将 boost::ptr_vector 包含在一个类 ( Zoo
) 中,并为其创建了一个 std::vector ( allZoos
)。看一个最小的代码来重现这个:
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/utility.hpp>
class Animal
{
public:
virtual char type() = 0;
};
class Cat : public Animal
{
public:
char type() { return 1; }
};
class Zoo
{
public:
boost::ptr_vector<Animal> animals;
};
int main()
{
std::vector<Zoo> allZoos;
Zoo ourCityZoo;
ourCityZoo.animals.push_back(new Cat());
//Uncommenting any of the lines below causes error:
//allZoos.push_back(ourCityZoo);
//allZoos.clear();
return 0;
}
声明allZoos
是可以的,但是调用它的任何成员函数都会导致编译器错误:(完整的错误日志太长了,没有贴出来)
C2259: 'Animal' : cannot instantiate abstract class c:\boost_1_49_0\boost\ptr_container\clone_allocator.hpp 34 1
这与 boost 的不可复制实用程序类和自定义new_clone
函数无关,我尝试了它们但没有运气。那怎么解决?
(我使用的是VS2010)