Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 简单C+中的错误+;程序_C++_Ubuntu 12.04 - Fatal编程技术网

C++ 简单C+中的错误+;程序

C++ 简单C+中的错误+;程序,c++,ubuntu-12.04,C++,Ubuntu 12.04,这是密码,如果有什么问题请帮助我 #include <iostream> int main() { char ch = 'A'; int num = ch; cout << "The ASCII code for " << ch << "is " << num << "\n"; cout << "Adding 1 to the character code : \n";

这是密码,如果有什么问题请帮助我

#include <iostream>

int main() {
    char ch = 'A';
    int num = ch;
    cout << "The ASCII code for " << ch << "is " << num << "\n";
    cout << "Adding 1 to the character code : \n";
    ch = ch + 1;
    num = ch;
    cout << "The ASCII code for " << ch << "is " << num << "\n";
    return (0);
}

请大家纠正我的错误。

使用
std::cout
或添加
std
名称空间。将以下内容放在文件顶部:

using namespace std;

问题是iostream头为您提供了这些对象,但仅在
std
名称空间下提供。使用限定名称,在其前面加上
std::

std::cout << code;

std::cout全局
cout
对象在
std
命名空间中定义(与标准库中的所有内容非常相似,只有少数例外)

因此,您可以完全限定名称(并使用
std::cout
):


这会将
std
命名空间中定义的所有符号导入全局命名空间,从而导致名称冲突的高风险。这是一种糟糕的编程实践,只能在有限的情况下使用。

cout
是在命名空间
std
中定义的流。因此,当您使用它时,您要么必须编写
std::cout
,要么在首次使用cout之前,您需要在全局范围内使用std::cout

建议的替代方案非常合适。使用
std::cout
No,No,不要再使用!为什么不在问一些琐碎的问题之前读一个C++教程?<代码>使用命名空间STD 通常不是一个坏主意。好吧,我明白了,你可能有一些深入的指针吗?在这个问题上有一个很好的讨论:我认为它不是一个函数。当然愚蠢的我。非常感谢。还有一个疑问,我们必须使用“using std::cout”和“using std::cin”声明像“cout”、“cin”这样的所有内容?有很多这样的声明,或者只有这两个?@prabuksara:这样声明所有需要的东西是一种很好的编程实践,只要这不会太冗长。当然,如果您正在使用的
std
命名空间中的名称太多,您可以使用
using namespace std
声明,但您应该将其放在有限的范围内(即函数内部),而不是(例如)共享头中的全局范围内。否则,导入该头的所有文件也将从
std
命名空间导入所有名称
std::cout << code;
std::cout << "The ASCII code for " << ch << "is " << num << "\n";
// ...
using std::cout;
cout << "The ASCII code for " << ch << "is " << num << "\n";
// ...
using namespace std;