kamikaze_pilot
kamikaze_pilot

Reputation: 14844

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....

Upvotes: 1

Views: 3166

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385405

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.

Upvotes: 7

Mat
Mat

Reputation: 206929

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.

Upvotes: 1

Related Questions