I have the following test code where I create a std::vector of class foo and later resize it.
#include <iostream>
#include <vector>
#include <algorithm>
class foo {
public:
foo(int n) : num(n) {}
//foo() : num(42) {}
~foo () { std::cout << "destructor" << std::endl; }
void print() { std::cout << num << std::endl; }
protected:
int num;
};
int main()
{
std::vector<foo> v = { 3,2,1,7,5,1,8,9 };
v.resize(2);
for (auto obj: v)
obj.print();
return 0;
}
When the default constructor is commented out, I get the the compilation error:
Error C2512 'foo::foo': no appropriate default constructor available
I compile using Visual Studio 2019, language is set to C++ 17. I don't understand this error message because in C++ >= 11, std::vector::resize has an the following overload:
void resize (size_type n)
so I'm supposed to be able to call it without a default value, or without there being a default constructor defined. What's going on?