从 'unsigned int' 到 'int' 的转换需要收缩转换

conversion from 'unsigned int' to 'int' requires a narrowing conversion

我的代码包括以下内容,我根据下面的最后一行得到上面的错误消息。

struct List {
    int word_i;
    int mod_i;
    char mod_type;
    char mod_char;
};

struct Morph {
    Options mode;
    deque<List> search_list;
    vector<string> dictionary;
    vector<bool> discovered;
    string output;
    int sel_word_i = 0;
    bool end_found = 0;
};

// later on in a function:
morph->search_list.push_back({ morph->dictionary.size() - 1, 0, 0, 0 });

您可以将最后一行替换为:

morph->search_list.emplace_back( morph->dictionary.size() - 1, 0, 0, 0 );

因此对象不是通过不允许收缩转换的大括号初始化创建的。

缩小转换是从调用的 return 值到 size which returns std::size_t which is std::size_t which is unsigned.

为什么 size() - 1 没有转换为有符号值,请参阅:C++ Implicit Conversion (Signed + Unsigned)