I write a abstract class foo
, and bar
class inherits from foo.
I want create a map container that is map<string, pair<string, foo&>>
, but I cannot compile successfully. The compiler tells me
“std::pair<std::string,foo &>::pair”: not appropriate default constructor
Here is code:
#include <iostream>
#include <string>
#include <windows.h>
#include <map>
#include <utility>
using namespace std;
class foo
{
public:
virtual void t() = 0;
};
class bar :public foo
{
public:
void t()
{
cout << "bar" << endl;
}
};
int main()
{
bar b;
//wrong
//map<string, pair<string, foo&>> t;
//pair<string, foo&> p("b", b);
//t["t"] = p;
//right
map<string, pair<string, foo*>> t;
pair<string, foo*> p("b", &b);
t["t"] = p;
p.second->t();
}
I want to know the difference between map<string, pair<string, foo*>>
and map<string, pair<string, foo&>>
.