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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffricCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
peprCommented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.