I have this class:
class MyClass {
public:
int someMember;
std::vector<int> someVectorMember;
};
and I was wondering why this wouldn't work:
MyClass(1, std::vector<int>{1,2});
Error:
main.cpp:21:37: error: no matching function for call to ‘MyClass::MyClass(int, std::vector)’
The problem is that you're passing two arguments of which the first is of type int
and the second is of type std::vector<int>
while there is no such constructor that takes two parameters of the mentioned types. Note also that in C++17, MyClass
aggregate initialization cannot work using parenthesis.
In C++20 though aggregate initialization can use parenthesis so this will work in c++20 as per p0960: Allow initializing aggregates from a parenthesized list of values .
why is there no default constructor provided
There is a default ctor MyClass::MyClass()
implicitly generated by the compiler, it's just that it cannot be used for MyClass(1, std::vector<int>{1,2})