0

My code includes the following, and I get the error message above based on the last line below.

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 });

1 Answers1

2

You can replace the last line with:

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

Thus the object is created not through brace initialization which does not allow narrowing conversion.

The narrowing conversion is from the return value of the call to size which returns std::size_t which is unsigned.

For why size() - 1 is not converted to a signed value see: C++ Implicit Conversion (Signed + Unsigned)

Amir Kirsh
  • 10,335
  • 31
  • 57
  • So that solved that error, but now I get the following: term does not evaluate to a function taking 1 argument – Francisco Skrobola Sep 16 '20 at 04:36
  • @FranciscoSkrobola - Read carefully. You need to change the member function being called from `push_back()` to `emplace_back()`. The two are not interchangeable when you're trying to construct and add and object. – Peter Sep 16 '20 at 04:57
  • I did change it but doing so caused the evaluation error – Francisco Skrobola Sep 16 '20 at 05:13