Search code examples
c++constructorvalue-type

No matching constructor for initialisation of 'value type'


I have got some code that gets the most frequent words and puts them into a vector. I then sort the vector into numerical order and all this works fine. I then try to resize the vector to 10 so I can get the top ten that I want to sort by word.

I think the problem lies with part of my struct but i am not to sure here is the code i am using.

struct wordFreq
{
    string word;
    int count;

    wordFreq(string str, int c): word(str),count(c) { }
}; 

words.resize(10);

Any help will be appreciated.


Solution

  • When resizing the vector, the function resize needs to know the value for the new elements. Therefore, the call

    words.resize(10);
    

    includes a default argument of the form wordFreq(), which is invalid in your case, since the class wordFreq doesn't have a default constructor.

    If no new elements are being created, use erase instead of resize.

    words.erase(words.begin() + 10, words.end());