c ++列表没有类型错误

时间:2011-04-10 15:23:59

标签: c++ list

所以我有这个代码:

    #include <list>

void j(){
    list<int> first;
}

然后我收到了这个错误:

error: ISO C++ forbids declaration of `list' with no type
error: expected `;' before '<' token

我做错了什么哈哈....

2 个答案:

答案 0 :(得分:7)

C ++标准库中的类型和函数位于std命名空间中。

这意味着您要查找的类型为std::list<int>


您可以避免在同一范围内使用以下任一方式编写std::

using namespace std;

using std::list;

(现在您只需编写list<int>,因为该类型已从<{1}}命名空间带入范围。)

前者有时是皱眉头;两者都应该在标题中避免。

答案 1 :(得分:1)

要么:

std::list<int> first;

或将using namespace std;放在功能上方的某处。所有标准容器都在std命名空间中声明,以避免使用用户代码命名冲突。

出于同样的原因,第一种方法(显式命名空间)更好一点,但这更多的是品味。