C++ C+中的智能指针和映射+;

C++ C+中的智能指针和映射+;,c++,dictionary,smart-pointers,C++,Dictionary,Smart Pointers,我正在尝试使用智能指针执行此程序: //File user.h #include <list> #include <iostream> #include <string> #include <fstream> #include <sstream> #include <memory> #include <map> using namespace std; class User { string name

我正在尝试使用智能指针执行此程序:

//File user.h
#include <list>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <memory>
#include <map>

using namespace std;

class User {
    string name;
    int id;
public:
    User(const string& name, int id) : name(name), id(id) {}
    int getID() const {return id;}
    ~User(){}
};

//File main.c
#include "user.h"
using namespace std;
typedef std::shared_ptr<User> UserPtr;
typedef map<string, UserPtr> Dict;

int main()
{
    Dict dict;
    dict = new User("Adams", 666);
    auto it = dict.find("Adams");

    if (it == dict.end())
        cout << "not found!" << endl;

    else
        cout << "id3: " << it->second->getID() << endl;

    return 0;
}
//文件user.h
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类用户{
字符串名;
int-id;
公众:
User(const string&name,int id):name(name),id(id){}
int getID()常量{return id;}
~User(){}
};
//文件main.c
#包括“user.h”
使用名称空间std;
typedef std::shared_ptr UserPtr;
typedef映射Dict;
int main()
{
Dict-Dict;
dict=新用户(“Adams”,666);
自动it=dict.find(“Adams”);
if(it==dict.end())

coutDict是std::map,您试图为它分配一个指向用户的指针,这是错误的

你需要的是

dict["Adams"] = std::make_shared<User>("Adams", 666);
dict[“Adams”]=std::make_shared(“Adams”,666);

您的地图没有正确添加元素。
map[key]=value;
dict["Adams"] = std::make_shared<User>("Adams", 666);