Here is my custom class, which I am using as a key. Next is the value, which I am using as vector. While I am trying to use uniform initialization, I get the compilation error. I am unable to understand what that error really means? Here is the error and the code.
Based on the advise by somebody on insert - How to use insert_or_assign in map for custom class?
I am using the similar thing for emplace(). However, it is not working.
The prototype of emplace says -
https://en.cppreference.com/w/cpp/container/map/emplace
template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );
It means the Args && is nothing but rvalue reference of class Args right?
In my case, the arg is nothing but std::vector. How can I do emplace?
class MyData
{
private:
int age;
string name;
public:
int getAge() const
{
return age;
}
string& getName()
{
return name;
}
MyData(int age_val, string name_val): age(age_val), name(name_val)
{
cout<<"Constructor invoked"<<endl;
}
bool operator <(const MyData &other) const
{
return age < other.age;
}
MyData(const MyData& other)
{
cout<<"Copy constructor invoked"<<endl;
age = other.age;
name = other.name;
}
};
int main(int argc, char **argv)
{
std::map<MyData, vector<string>> studentClasses;
studentClasses.emplace({32, "SJ"s}, std::vector{"working"s});
return 0;
}
In function ‘int main(int, char**)’:
map.cpp:62:63: error: no matching function for call to ‘std::map<MyData, std::vector<std::__cxx11::basic_string<char> > >::emplace(<brace-enclosed initializer list>, std::vector<std::__cxx11::basic_string<char> >)’
62 | studentClasses.emplace({32, "SJ"s}, std::vector{"working"s});
574 | emplace(_Args&&... __args)
| ^~~~~~~
/usr/include/c++/9/bits/stl_map.h:574:2: note: candidate expects 0 arguments, 2 provided
Aucun commentaire:
Enregistrer un commentaire