std::vector
이 boost::ptr_vector
이어야합니다. 관리를 쉽게하기 위해 boost :: ptr_vector를 클래스 (Zoo
)에 넣고 std :: vector로 만들었습니다 (allZoos
). 이 문제를 재현하기위한 최소한의 코드를보고 : (전체 오류 로그가 게시되지, 너무 오래했다)ptr_vector <an_abstract_class>가 포함 된 클래스의 벡터를 만들 수 없습니다
C2259: 'Animal' : cannot instantiate abstract class c:\boost_1_49_0\boost\ptr_container\clone_allocator.hpp 34 1
:
#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
괜찮 선언,하지만 멤버 함수를 호출하면 컴파일러 오류가 발생합니다
이것은 부스트의 복사 할 수없는 유틸리티 클래스와 사용자 정의 new_clone
함수와 아무런 관련이 없습니다. 어떻게 해결 될 수 있습니까?
(내가 VS2010를 사용하고 있습니다)
가장 쉬운 해결책은 아마도'ptr_vector'를 버리고 대신'std :: vector>'를 사용하는 것입니다. –
Xeo
@ Xeo : 예, 지금은 임시 해결 방법을 사용하고 있습니다. 그러나 이후에'ptr_vector'가 그러한 템플릿을 대체하기 위해 만들어졌고 "작동"해야합니다! 나는 또한 왜 std :: vector>'가 단순히 작동하는지 궁금하지만 질문의 코드는 그렇지 않다. –
Hossein