fork download
  1. #include <vector>
  2.  
  3. struct X { X(int i) { x = i; }; int x; };
  4. struct Y { int y; };
  5.  
  6. int main()
  7. {
  8. const int n = 4;
  9. int k = 4;
  10.  
  11. X x1[n]; // compile error: no default constructor
  12. X x2[n] = { 1, 2, 3, 4};
  13. Y y1[n];
  14. Y y2[k]; // compile error: k is non-const
  15.  
  16. X *xp = new X[k]; // compile error: no default constructor
  17. Y *yp = new Y[k];
  18.  
  19. std::vector<X> xv(10); // compile error: no default constructor
  20. std::vector<Y> yv(10);
  21.  
  22. delete [] yp;
  23. return 0;
  24. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:11:9: error: no matching function for call to 'X::X()'
   X x1[n];                // compile error: no default constructor
         ^
prog.cpp:11:9: note: candidates are:
prog.cpp:3:12: note: X::X(int)
 struct X { X(int i) { x = i; }; int x; };
            ^
prog.cpp:3:12: note:   candidate expects 1 argument, 0 provided
prog.cpp:3:8: note: X::X(const X&)
 struct X { X(int i) { x = i; }; int x; };
        ^
prog.cpp:3:8: note:   candidate expects 1 argument, 0 provided
prog.cpp:16:18: error: no matching function for call to 'X::X()'
   X *xp = new X[k];       // compile error: no default constructor
                  ^
prog.cpp:16:18: note: candidates are:
prog.cpp:3:12: note: X::X(int)
 struct X { X(int i) { x = i; }; int x; };
            ^
prog.cpp:3:12: note:   candidate expects 1 argument, 0 provided
prog.cpp:3:8: note: X::X(const X&)
 struct X { X(int i) { x = i; }; int x; };
        ^
prog.cpp:3:8: note:   candidate expects 1 argument, 0 provided
prog.cpp: In constructor 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = X; _Alloc = std::allocator<X>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::value_type = X; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<X>]':
prog.cpp:19:23: error: no matching function for call to 'X::X()'
   std::vector<X> xv(10);  // compile error: no default constructor
                       ^
prog.cpp:19:23: note: candidates are:
prog.cpp:3:12: note: X::X(int)
 struct X { X(int i) { x = i; }; int x; };
            ^
prog.cpp:3:12: note:   candidate expects 1 argument, 0 provided
prog.cpp:3:8: note: X::X(const X&)
 struct X { X(int i) { x = i; }; int x; };
        ^
prog.cpp:3:8: note:   candidate expects 1 argument, 0 provided
prog.cpp:19:23: note:   when instantiating default argument for call to std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = X; _Alloc = std::allocator<X>; std::vector<_Tp, _Alloc>::size_type = unsigned int; std::vector<_Tp, _Alloc>::value_type = X; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<X>]
   std::vector<X> xv(10);  // compile error: no default constructor
                       ^
stdout
Standard output is empty