#include <iostream>
#include <vector>
struct foo {
virtual void print() =0;
};
struct goo : public foo {
int a;
void print() { std::cout << "goo"; }
};
struct moo : public foo {
int a,b;
void print() { std::cout << "moo"; }
};
int main() {
std::vector<foo> foos;
foos.push_back(moo());
foos.push_back(goo());
foos.push_back(goo());
foos.push_back(moo());
for(foo& f : foos) {
f.print();
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKc3RydWN0IGZvbyB7Cgl2aXJ0dWFsIHZvaWQgcHJpbnQoKSA9MDsKfTsKCnN0cnVjdCBnb28gOiBwdWJsaWMgZm9vIHsKCWludCBhOwoJdm9pZCBwcmludCgpIHsgc3RkOjpjb3V0IDw8ICJnb28iOyB9Cn07CgpzdHJ1Y3QgbW9vIDogcHVibGljIGZvbyB7CglpbnQgYSxiOwoJdm9pZCBwcmludCgpIHsgc3RkOjpjb3V0IDw8ICJtb28iOyB9Cn07CgppbnQgbWFpbigpIHsKCXN0ZDo6dmVjdG9yPGZvbz4gZm9vczsKCWZvb3MucHVzaF9iYWNrKG1vbygpKTsKCWZvb3MucHVzaF9iYWNrKGdvbygpKTsKCWZvb3MucHVzaF9iYWNrKGdvbygpKTsKCWZvb3MucHVzaF9iYWNrKG1vbygpKTsKCQoJZm9yKGZvbyYgZiA6IGZvb3MpIHsKCQlmLnByaW50KCk7Cgl9CglyZXR1cm4gMDsKfQ==
In file included from /usr/include/i386-linux-gnu/c++/4.8/bits/c++allocator.h:33:0,
from /usr/include/c++/4.8/bits/allocator.h:46,
from /usr/include/c++/4.8/string:41,
from /usr/include/c++/4.8/bits/locale_classes.h:40,
from /usr/include/c++/4.8/bits/ios_base.h:41,
from /usr/include/c++/4.8/ios:42,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from prog.cpp:1:
/usr/include/c++/4.8/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = foo; _Args = {foo}; _Tp = foo]’:
/usr/include/c++/4.8/bits/alloc_traits.h:254:4: required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = foo; _Args = {foo}; _Alloc = std::allocator<foo>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8/bits/alloc_traits.h:393:57: required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = foo; _Args = {foo}; _Alloc = std::allocator<foo>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8/bits/vector.tcc:97:40: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {foo}; _Tp = foo; _Alloc = std::allocator<foo>]’
/usr/include/c++/4.8/bits/stl_vector.h:920:36: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = foo; _Alloc = std::allocator<foo>; std::vector<_Tp, _Alloc>::value_type = foo]’
prog.cpp:20:22: required from here
/usr/include/c++/4.8/ext/new_allocator.h:120:4: error: cannot allocate an object of abstract type ‘foo’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
prog.cpp:4:8: note: because the following virtual functions are pure within ‘foo’:
struct foo {
^
prog.cpp:5:15: note: virtual void foo::print()
virtual void print() =0;
^
In file included from /usr/include/c++/4.8/vector:62:0,
from prog.cpp:2:
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = foo; _Args = {foo}]’:
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<foo*>; _ForwardIterator = foo*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<foo*>; _ForwardIterator = foo*]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<foo*>; _ForwardIterator = foo*; _Tp = foo]’
/usr/include/c++/4.8/bits/stl_uninitialized.h:281:69: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = foo*; _ForwardIterator = foo*; _Allocator = std::allocator<foo>]’
/usr/include/c++/4.8/bits/vector.tcc:415:43: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {foo}; _Tp = foo; _Alloc = std::allocator<foo>]’
/usr/include/c++/4.8/bits/vector.tcc:101:54: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {foo}; _Tp = foo; _Alloc = std::allocator<foo>]’
/usr/include/c++/4.8/bits/stl_vector.h:920:36: required from ‘void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = foo; _Alloc = std::allocator<foo>; std::vector<_Tp, _Alloc>::value_type = foo]’
prog.cpp:20:22: required from here
/usr/include/c++/4.8/bits/stl_construct.h:75:7: error: cannot allocate an object of abstract type ‘foo’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
prog.cpp:4:8: note: since type ‘foo’ has pure virtual functions
struct foo {
^