开发者

Compiler error with list<string>

I'm trying to create a list of strings, following the example here. This below gives me syntax errors:

private: list<string开发者_如何学Python> images;

The errors (all on the line where the above declaration is):

syntax error : missing ';' before '<'
missing type specifier - int assumed. Note: C++ does not support default-int
unexpected token(s) preceding ';'

It's in a class with only a single constructor besides it, and it compiles fine without it. What am I doing wrong?


Did you #include both <list> and <string>? Also, did you import the names list and string from namespace std by writing either

using namespace std;

or

using std::list;   using std::string;

The error you're getting is consistent with the names not being accessible, so this is my best guess.

EDIT: Since this is in a header file, you should not be using either of the above constructs (thanks to wilhelmtell for pointing out that this is a header file!). Instead, you should fully-qualify the names as

private: std::list<std::string> images;

This way the compiler knows exactly where to look for list and string.


You need to qualify the list and string types with their namespace.

Either type std::list<std::string> or add using namespace std; after the #include <string> and #include <list> directives.

A simple working program:

#include <list>
#include <string>
using namespace std;
int main ( int, char ** )
{
    list<string> strings;
    strings.push_back("1st string");
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜