使用 std::list 时编译器错误
Compiler error with using std::list
我有一个简单的函数来检查一个值是否在列表中:
template <class T>
bool IsinList(list<T> l, T x)
{
for(list<T>::iterator it=list.begin(); it != list.end(); it++)
{
if (*it == x)
return true;
}
return false;
}
我在同一个.cpp文件中使用了该函数,如下所示:
if (!IsinList (words, temp))
goodwords.push_back(temp);
但是我收到此错误:
'std::list' : use of class template requires template argument list
我无法弄清楚问题是什么。我检查了以前的问题,但没有帮助。你能向我解释我做错了什么吗?
那里有
错别字:
list.begin() / list.end()
应该是
l.begin() / l.end()
您的变量称为 l
,而不是 list
。
编辑:
正如马蒂尼奥所指出的,这可能还不够。一些编译器会接受这一点,但由于迭代器依赖于模板参数,因此您可能需要一个类型名:
typename list<T>::iterator it=list.begin()
你打错了字(list
vs. l
),并且没有指定list<T>::iterator
是typename
。此外,您应该通过引用 const 传递list
和搜索参数。总而言之,它应该看起来像这样:
template <class T>
bool IsinList(const list<T>& l, const T& x)
{
typename list<T>::const_iterator first = l.begin(), last = l.end();
for(; first != last; ++first)
{
if (*it == x)
return true;
}
return false;
}
也就是说,仍然不要使用它。改用std::find
要好得多
if (std::find(words.begin(), words.end(), temp)==words.end())
{
goodwords.push_back(temp);
}
相关文章:
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 编译器错误:destuctor 的更宽松的抛出说明符
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- C++ G++ 编译器 - 错误:隐式声明的定义
- 这个失败的测试是将零添加到空指针未定义的行为、编译器错误还是其他什么?
- 模板模板参数和模板别名:编译器错误?
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 编译器错误(英特尔并行工作室 2019 与 Visual Studio 社区 2019)
- 如何在C++中克服此 C4430 编译器错误?
- 尝试使用 WinBGI 显示文本时出现编译器错误
- 使用typedef'ed返回类型声明友元函数时出现编译器错误
- 如何在类模板的成员函数中正确调用函数对象?正在生成 Visual Studio 编译器错误 C2440
- C++自定义分配器大小参数作为模板参数会引发编译器错误