User defined class in std::map

I have some code where I want to use a user defined class (MyDataSet) in a std::map<int,MyDataSet>.  Do I need to do something to make my class usable in a map?  I'm getting this error
C:/MinGW/x86_64-w64-mingw32/include/c++/tuple:1088:70: error: no matching function for call to 'MyDataSet::MyDataSet()'

Open in new window

Here's the code (as a simplified example):
#include <map>
#include <string>
using namespace std;

class MyDataSet {
public:
    int sessionid;
    string name;
    //Will add other instance variables
    MyDataSet(int, string);
};

MyDataSet::MyDataSet(int id, string n) {
    	sessionid = id;
	name = n;
}

int main () {
	MyDataSet myData{1, "two"};
	map<int,MyDataSet> myMap;
	myMap[42] = myData;
        exit(0);
}

Open in new window

anAppBuilderAsked:
Who is Participating?
 
phoffricConnect With a Mentor Commented:
Try adding MyDataSet() {} to your class.
According to http://www.cplusplus.com/reference/map/map/map/ there are 3 constructors for the map, and you are starting off with an empty map. That constructor is #1, the default constructor.
0
 
peprConnect With a Mentor Commented:
You can insert the pair like that:
    myMap.insert({ 43, myData });

Open in new window

In C++11, or you can probably use new .emplace() method like:
    myMap.emplace(42, myData);

Open in new window

Or you can combine construction of the object and insertion into the map:
    myMap.insert({ 42, { 1, "two" } });

Open in new window

0
 
phoffricCommented:
It seems inconsistent in the C++11 spec that for
myMap[42] = myData;
you need the default constructor, but for the three C++11 insert methods that pepr showed, you don't need the default constructor.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
anAppBuilderAuthor Commented:
Thank you both,  Both provided very useful information.
0
 
peprCommented:
This was useful also for me. (As usual, I had to RTFM to know how to do that ;) Anyway, I like the C++11 enhancements very much.

Actually, if you want to initialize the map in the source code (that is not to fill it dynamically), there is even the syntactically nicer way (more readable). The full example that I tried looks like this:
#include <map>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

class MyDataSet
{
public:
    MyDataSet(int, std::string);
    std::string to_string();

private:
    int sessionid;
    std::string name;
};


MyDataSet::MyDataSet(int id, std::string n)
{
    sessionid = id;
    name = n;
}


std::string MyDataSet::to_string()
{
    ostringstream oss;
    oss << "Session id: " << sessionid << "; Name: " << name;
    return oss.str();
}



int main () {
    MyDataSet myData{ 1, "two" };
    map<int, MyDataSet> myMap;
    myMap.insert({ 42, myData });
    myMap.emplace(43, myData);
    myMap.insert({ 44, { 2, "two" } });

    for (auto &item: myMap)
        cout << "Key: " << item.first << "; " << item.second.to_string() << endl;

    cout << "-----------------------------------------------\n";

    // It is also possible to initialize the map like this:

    map<int, MyDataSet> myMapInitialized{
        { 42, {1, "life, universe, and everything" } },
        { 43, { 2, "two" } },
        { 44, { 3, "three" } },
        { 45, { 4, "four" } },
    };

    for (auto &item : myMapInitialized)
        cout << "Key: " << item.first << "; " << item.second.to_string() << endl;

    return 0;
}

Open in new window

And it prints:
Key: 42; Session id: 1; Name: two
Key: 43; Session id: 1; Name: two
Key: 44; Session id: 2; Name: two
-----------------------------------------------
Key: 42; Session id: 1; Name: life, universe, and everything
Key: 43; Session id: 2; Name: two
Key: 44; Session id: 3; Name: three
Key: 45; Session id: 4; Name: four

Open in new window

0
 
anAppBuilderAuthor Commented:
Nice, TY again
0
All Courses

From novice to tech pro — start learning today.