Hello, I have an error I think with the initialization of a vector
error: no matching function for call to ‘City::City()’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
The constructor for City class take two parameters
thanks
1 2
int numCities = 100;
vector <City> cities(numCities);
#include <iostream>
#include <vector>
class City {
public:
int first {};
int second {};
// City() = default;
City(int first_arg, int second_arg);
private:
};
City::City(int first_arg, int second_arg)
: first { first_arg }
, second { second_arg }
{
}
int main()
{
constexprint Num_Cities { 100 };
std::vector<City> cities(Num_Cities);
}
There are at least two workarounds:
1) Reserve space by std::vector::reserve() instead of the overloaded std::vector(size_type count, const Allocator& alloc = Allocator()) constructor:
#include <iostream>
#include <vector>
class City {
public:
int first {};
int second {};
// City() = default;
City(int first_arg, int second_arg);
private:
};
City::City(int first_arg, int second_arg)
: first { first_arg }
, second { second_arg }
{
}
int main()
{
constexprint Num_Cities { 100 };
std::vector<City> cities;
cities.reserve(Num_Cities);
}
2) Add a suitable no-argument constructor to class City:
#include <iostream>
#include <vector>
class City {
public:
int first {};
int second {};
City() = default;
City(int first_arg, int second_arg);
private:
};
City::City(int first_arg, int second_arg)
: first { first_arg }
, second { second_arg }
{
}
int main()
{
constexprint Num_Cities { 100 };
std::vector<City> cities(Num_Cities);
}
std::vector::reserve() like Enoizat mentioned is probably what you want to be doing but you can also do this if you want all members of the vector to be constructed with the same two constructor arguments: