2009-09-20 130 views
2

我的C++是有点生疏所以...这个C++代码有什么问题?

#include<list> 
typedef list<int> foo; 

,让我的哦,这么好的错误消息:

TEST.CPP:2:`之前语法错误;”令牌

到底能我连谷歌在这...

回答

7

您期待列表位于全局命名空间中。但是在std命名空间内定义。因此,你应该使用using namespace std;或明确指定名称空间为std::list;我个人更喜欢第二个选项。

+1

不要忘记使用声明:“using std :: list;”。如果我引用了很多东西(cout,endl),我会将它们带入当前范围,而不会将std的其余部分 – Bill 2009-09-20 14:36:22

14

C++标准库的名字在命名空间std

#include <list> 
typedef std::list<int> foo; 
5

list<>什么是性病的命名空间。这应该工作得很好:

#include<list> 
typedef std::list<int> foo; 
+1

Drat。太慢了。 :( – greyfade 2009-09-20 06:20:59

0

或者你可以做,

#include<list> 
using namespace std; 
typedef list<int> foo; 

,如果你不想键入std::无处不在。

+0

实际上,我认为这个不好的建议(我不想从这里开始讨论名义空间的争论,但我坚持我的意见。) – sbi 2009-09-20 20:58:14