简体   繁体   English

C++<iostream.h> 错误

[英]C++ <iostream.h> Error

I am at the absolutely newest level of new when it comes to C++.当谈到 C++ 时,我处于绝对最新的 new 级别。 It may seem like a noob mistake, but I think I'm missing something with my first program, "Hello World!".这似乎是一个菜鸟错误,但我认为我的第一个程序“Hello World!”遗漏了一些东西。

I'm running from Ubuntu (not sure if this is any different from working with Windows), and I'm using a book called Teach Yourself C++ in 21 Days .我在 Ubuntu 上运行(不确定这与使用 Windows 是否有任何不同),我正在使用一本名为Teach Yourself C++ in 21 Days 的书

The code I'm resembling looks exactly like this:我相似的代码看起来完全像这样:

#include <iostream.h>
int main()
{
cout <<"Hello World!\n";
    return 0;
}

I have this exactly in my text editor, but I keep getting greeted by the same error whenever I try to compile it!我的文本编辑器中正好有这个,但是每当我尝试编译它时,都会遇到同样的错误!

first.cpp:2:22: fatal error: iostream.h: No such file or directory compilation terminated. first.cpp:2:22: 致命错误: iostream.h: 没有这样的文件或目录编译终止。

I'm pretty distressed as this is literally the first step in my coding career!我很苦恼,因为这实际上是我编码生涯的第一步! I'm not sure if ubuntu needs to be treated differently than Windows (which is what the book is using as reference).我不确定 ubuntu 是否需要与 Windows 区别对待(这就是本书用作参考的内容)。

Help!帮助!

There are two problems here:这里有两个问题:

You need to omit the .h suffix:您需要省略.h后缀:

#include <iostream>

Also, cout is an unqualified name, and needs to be qualified with the std namespace since you are not using namespace std :此外, cout是一个非限定名称,需要使用std命名空间进行限定,因为您没有using namespace std

std::cout << "Hello World!\n";

There shouldn't be any iostream.h it's simply called iostream and should look like this:不应该有任何iostream.h它只是被称为iostream并且应该是这样的:

#include <iostream>
int main()
{
std::cout <<"Hello World!\n";
    return 0;
}

(also notice the std:: before the cout, since it means that it's from the standard namespace.) (还要注意 cout 之前的std:: ,因为这意味着它来自标准命名空间。)

You want just iostream你只想要 iostream

#include <iostream>

I suspect the book is very old.我怀疑这本书很旧了。 Names are qualified in the std namespace, so you may want to add名称在 std 命名空间中被限定,因此您可能需要添加

using namespace std;

For now at least.至少现在是这样。

use采用

#include <iostream>

in general STL header file don't have a .h通常 STL 头文件没有 .h

you need to compile using g++ compiler, not by gcc您需要使用 g++ 编译器进行编译,而不是使用 gcc

g++ hello.cpp g++ hello.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM