没有匹配的构造函数初始化'值类型' [英] No matching constructor for initialisation of 'value type'
问题描述
我有一些代码,获取最常见的单词,并将它们放入一个向量。然后我将向量按数字顺序排序,这一切都很好。然后我尝试将向量调整为10,所以我可以得到前十个,我想通过词排序。
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.
推荐答案
调整向量大小时,函数 resize
需要知道新元素的值。因此,调用
When resizing the vector, the function resize
needs to know the value for the new elements. Therefore, the call
words.resize(10);
包含 wordFreq()
,在您的情况下是无效的,因为 wordFreq
类没有默认构造函数。
includes a default argument of the form wordFreq()
, which is invalid in your case, since the class wordFreq
doesn't have a default constructor.
如果没有创建新元素,请使用 erase
而不是 resize
。
If no new elements are being created, use erase
instead of resize
.
words.erase(words.begin() + 10, words.end());
这篇关于没有匹配的构造函数初始化'值类型'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!