【问题标题】:Map with an inner stuct builder使用内部结构构建器映射
【发布时间】:2016-08-08 13:33:31
【问题描述】:

我有一张内部结构如下的地图:

struct Messages {
    unsigned int id;
    string user_name;
    string content;
    time_t timestamp;

    Messages(const string& a_user_name, const string& a_content) :
        user_name(a_user_name), content(a_content), id(++last_id)
    {
        timestamp = time(0);
    }
};

map<unsigned int, Messages> Global_messages;

当我尝试访问元素时:

cout << Global_messages[i].id;

它给了我一个关于没有合适的构建器的错误。

【问题讨论】:

  • 获取准确的错误消息和映射/结构初始化代码会很有帮助。另外,是否有 Global_messages[i] 值?
  • 我已经使用默认构建器解决了它

标签: c++ dictionary struct builder


【解决方案1】:

std::map::operator[] 要求该值是默认可构造的,Messages 不是这种情况。您必须使用其他接口作为insert/emplace 来填充地图和atfind 来检索值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-24
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多