C++ template for QList
C++ template for QList
我的模板函数:
template<typename TList, typename TItem>
void setIndex(TList &list, QString &selectedId)
{
typename QList<TItem>::iterator iter;
for (iter = list.begin(); iter != list.end(); iter++){
if(iter->id() == selectedId){
m_curItemIdx = iter - list.begin();
break;
}
}
}
我试着打电话
setIndex<>(list, selectedId);
但是此调用会出错。
error: no matching member function for call to 'setIndex' (...)
note: candidate template ignored: couldn't infer template argument 'TItem'
所以我尝试了
setIndex<QList<Item>, Item>(list, selectedId);
然后它奏效了。我仍然不清楚为什么第一个失败了。
(编辑(
在另一个功能的情况下,
template<typename TList, typename TItem>
void addItem(TList &list, TItem &item)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
list << item;
endInsertRows();
}
对于此函数,
addItem<>(list, item)
工作。我不是在解释是什么有什么不同
你可能想要(所以可以进行演绎(:
template<typename TItem>
void setIndex(const QList<TItem>& list, const QString &selectedId)
{
typename QList<TItem>::iterator iter;
for (iter = list.begin(); iter != list.end(); ++iter) {
if (iter->id() == selectedId) {
m_curItemIdx = iter - list.begin();
break;
}
}
}
或
template <typename Container>
void setIndex(const Container& c, const QString& selectedId)
{
auto it = std::find_if(c.begin(),
c.end(),
[&](const auto& item){ return item.id() == selectedId });
if (it != c.end()) {
m_curItemIdx = std::distance(c.begin(), it);
}
}
相关文章:
- "error: no matching function for call to"构造函数错误
- 表示"accepting anything for this template argument" C++概念的通配符
- 如何在C++中从两个单独的for循环中添加两个数组
- 在Linux for Windows上编译C++代码时出错
- 调用专用模板时出错"no matching function for call to [...]"
- 为什么我的for循环不能正确获取argv
- 为什么我不能在 FOR LOOP 中使用 i/10,C++?
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 在基于范围的for循环中使用结构化绑定声明
- 通过for循环使用用户输入填充列表
- 使用for循环检查数组中的重复项
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 正在使用for循环创建QScatterSerie
- Python中的for循环与C++有何不同
- std::memory_order for std::atomic:<T>:wait
- 在更改for循环的第三部分后,未使用for循环结果
- 在 for 循环中查找问题时遇到困难
- C++ template for QList
- Qt foreach循环排序与QList的for循环排序