This question already has an answer here:
- c++ “no matching function for call to” error with structure 2 answers
DISCLAIMER: I crafted the MCVE to demonstrate an issue I had with a much larger project that has nothing to do with the weather. I chose the MCVE's names and program goal only to demonstrate the problem in an easy-to-understand way. I know this design would suck for a real-life forecast program.
I am using a std::map
to store structs, using std::string
as a key.
struct Temps
{
//Temps(int l = 0, int h = 0)
Temps(int l, int h)
:low(l), high(h)
{}
int low;
int high;
};
int main()
{
std::map<std::string, Temps> forecast;
forecast.emplace("Monday", Temps(49, 40));
forecast.emplace("Tuesday", Temps(66, 35));
forecast.emplace("Wednesday", Temps(78, 40));
return 0;
}
However, I'm having a problem. I can access the contents of those structs within the map using iterators, as expected...
std::map<std::string, Temps>::iterator it;
it = forecast.find("Monday");
if(it != forecast.end())
{
std::cout << "Monday's high is " << it->second.high << "." << std::endl;
}
But when I try to access using the []
operator on the map, I run into an error...
if(forecast.find("Tuesday") != forecast.end())
{
std::cout << "Tuesday's high is " << forecast["Tuesday"].high << std::endl;
}
I get a massive, hideous error, which summarizes down to...
/usr/include/c++/5/tuple|1172|error: no matching function for call to ‘Temps::Temps()’|
I know the problem is neither with the struct or the data. What's going on?