开发者

c++ list no type error

so I had this code:

    #include <list>

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

but then I get this error:

error: ISO C++ forbids declaration of `list' with no type
error: expected `;' before '<'开发者_运维百科 token

what did I do wrong lol....


Types and functions in the C++ Standard Library are in the std namespace.

This means that the type you are looking for is std::list<int>.


You can avoid having to write std:: by using either of the following in the same scope:

using namespace std;

or

using std::list;

(Now you can just write list<int>, because the type has been brought into scope from the std namespace.)

The former is sometimes frowned-upon; both ought to be avoided in headers.


Either do:

std::list<int> first;

or put using namespace std; somewhere above your function. All the standard containers are declared in the std namespace to avoid naming clashes with user code.

The first method (explicit namespace) is a bit better for the same reason, but that's more a matter of taste.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜