Class Foo only has a default constructor and a copy constructor. A vector of size 10 initialized by object of type Foo is somehow incorrect.
#include <iostream>
#include <vector>
class Foo
{
public:
Foo() = default; // Error1
Foo(Foo& foo) { // Error2
std::cout << "copied" << std::endl;
}
};
int main( void )
{
Foo f;
std::vector<Foo> vec(10, f); // Error3
return 0;
}
There are 3 errors for the sample code above:
Error1: candidate constructor not viable: requires 0 arguments, but 1 was provided
Error2: candidate constructor not viable: 1st argument ('const Foo') would lose const qualifier
Error3: in instantiation of member function 'std::__1::vector >::vector' requested here
When I remove the copy constructor or the vector in the main function, there are no errors.
Question:
Which part of the code is wrong and why?
p.s.
When the vector definition is replaced by Foo ff(f), errors are gone as well. Is the const qualifier is request by the vector?
Foo(const Foo& foo)std::vectoraddsconstbefore copying to ensure the initial object is not changed in some weird copy constructors like the ones juanchopanza had in mind while posting his comment.