fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct foo {
  5. virtual void print() =0;
  6. };
  7.  
  8. struct goo : public foo {
  9. int a;
  10. void print() { std::cout << "goo"; }
  11. };
  12.  
  13. struct moo : public foo {
  14. int a,b;
  15. void print() { std::cout << "moo"; }
  16. };
  17.  
  18. int main() {
  19. std::vector<foo> foos;
  20. foos.push_back(moo());
  21. foos.push_back(goo());
  22. foos.push_back(goo());
  23. foos.push_back(moo());
  24.  
  25. for(foo& f : foos) {
  26. f.print();
  27. }
  28. return 0;
  29. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
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 {
        ^
stdout
Standard output is empty