'list'未在此范围中声明 [英] 'list' was not declared in this scope

查看:234
本文介绍了'list'未在此范围中声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是c ++的新手,我试图得到一个基本的程序来初始化一个无符号整数的列表。我正在使用scygwin和g ++编译和运行。



下面是.cpp文件中的代码:

  #include< iostream> 
#include< fstream>
#include< sstream>
#include< string>
#include< typeinfo>
using namespace std;


int main(int argc,char * argv []){

list< int>事件;

return 0;
}

我通过在cygwin终端输入以下命令运行:

  $ g ++ -o test.out test.cpp 

$ b然而,我得到以下编译错误:


test.cpp:在函数'int main int,char **)':test.cpp:16:1:错误:
'list'未在此范围列表事件中声明;

^
test.cpp: 16:6:错误:'int'之前的预期主表达式
列出事件;
^


我很困惑为什么列表不在范围内,因为我使用命名空间std?我发现一个类似的问题,关于这个在一个c ++论坛,但我的问题会解决这一点。

解决方案

任何人都知道这是什么问题吗?



< using namespace std; 不会为您的代码添加任何功能。它只是意味着你不必键入 std :: 引用 std 命名空间中的东西,如 std :: list



实际包含 std :: list 到您的程序中,您需要添加:

  #include< list& 

当对这种事情有疑问时,在google搜索 cpp参考列表将打开一个页面,如您可以看到:定义在顶部的标题< list> 中。



这里是关于的另一个问题 using namespace std; 这可能证明是有用的,为什么不应该使用它。我将添加一些来解释命名空间。



在C ++程序中,将函数组织成类和命名空间是很常见的。想象一下,你写了自己的 list 类来处理某些情况。为了防止命名冲突,您可以将其放在与 std 不同的命名空间中。

  namespace MyApp {
class list;
void sort(list&);
}

对于大型代码库的大多数,您可能仍然希望使用 std :: list 但是你需要 MyApp :: list 某些东西。使用命名空间,您可以将代码聚集并防止类似功能的命名冲突。



摘要



using namespace std; 使得如果你引用一个不在全局命名空间中的函数或类,它会在 std 命名空间。



#include< list> 实际插入原型在预处理器阶段访问源文件中的代码)。


I am new to c++, and I am trying to get a basic program to initialize a list of short unsigned integers. I am compiling and running using scygwin and g++.

Below is the code in the .cpp file:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;


int main (int argc, char* argv[]) {

list<int> events;

return 0;
}

which I run by typing the following command into cygwin terminal:

$ g++ -o test.out test.cpp

However, I get the following compilation errors:

test.cpp: In function ‘int main(int, char**)’: test.cpp:16:1: error: ‘list’ was not declared in this scope list events;
^ test.cpp:16:6: error: expected primary-expression before ‘int’ list events; ^

I am confused about why list is not in the scope, since I am using namespace std? I found a similar question asked about this on a c++ forum, but my problem would be resolved with that. Anyone know what the problem is here?

-Paul

解决方案

using namespace std; doesn't add any functionality to your code. It just means you don't have to type std:: when referencing things in the std namespace, like std::list.

To actually include the code base for std::list into your program, you need to add:

#include <list>

When in doubt about this kind of thing, doing a google search for cpp reference list will turn up a page like this where you can see: Defined in header <list> at the top.

Here's another question about using namespace std; that may prove useful and why you shouldn't use it. I'll add a little bit to perhaps explain namespaces.

It is common in C++ programs to organize functions into classes and namespaces. Imagine you wrote your own list class to handle certain scenarios. In order to prevent naming conflicts you would put it in a different namespace than std.

namespace MyApp {
    class list;
    void sort(list&);
}

For the majority of a large code base you might still prefer to use std::list but you need MyApp::list for some things. Using namespaces you can cluster your code and prevent naming conflicts for similar functionality.

Summary

using namespace std; makes it so that if you reference a function or class not in the global namespace it looks for it in the std namespace.

#include <list> actually inserts prototypes (information about how to access the code) in your source file during the preprocessor stage.

这篇关于'list'未在此范围中声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆