[英]Why doesn't a simple "Hello World"-style program compile with Turbo C++?
I have started learning C++ for my programming class.我已经开始为我的编程课学习 C++。 I have downloaded this "Hello World" program:我已经下载了这个“Hello World”程序:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
but Turbo C++ complains:但是 Turbo C++ 抱怨:
Error D:\HELLO.CPP 1: Unable to open include file 'IOSTREAM'
Error D:\HELLO.CPP 2: Declaration syntax error
Error D:\HELLO.CPP 6: Undefined symbol 'cout'
What's wrong with this very simple program?这个非常简单的程序有什么问题? How can I correct these errors?我该如何纠正这些错误?
There's no problem with this program.这个程序没有问题。 (Except probably some stylistic issues — using namespace std
is not recommended). (除了一些风格问题——不推荐using namespace std
)。 The problem is with Turbo C++.问题在于 Turbo C++。 It is a very old piece of software.这是一个非常古老的软件。 It implements a dialect of C++, so-called pre-ANSI C++ , that has completely fallen out of use by the beginning of this millennium.它实现了一种 C++ 方言,即所谓的pre-ANSI C++ ,到本世纪初已经完全停止使用。 The first ANSI standard for C++ was published in 1998, then there was the 2003 version, the 2011 version, the 2014 version, the 2017 version, and now we expect the 2020 version to be officially published. C++的第一个ANSI标准于1998年发布,然后是2003版、2011版、2014版、2017版,现在我们预计2020版将正式发布。 Each of these standard revisions brought more or less significant changes to the language.这些标准修订中的每一个都或多或少地对语言带来了重大变化。
For Turbo C++ you have to modify the program like this:对于 Turbo C++,您必须像这样修改程序:
#include <iostream.h> // note the .h suffix
// using namespace std; // Turbo C++ doesn't implement namespaces
int main()
{
cout << "Hello, World!";
return 0;
}
If you look at this program, the difference between the modern C++ dialect and the one accepted by Turbo C++ may seem small.如果你看看这个程序,现代 C++ 方言和 Turbo C++ 接受的方言之间的差异可能看起来很小。 However it will grow much larger as your programs will be getting more complex.但是,随着您的程序变得越来越复杂,它会变得更大。
While you can learn programming using Turbo C++ I would strongly recommend to avoid that if humanly possible because of the following problems:虽然您可以使用 Turbo C++ 学习编程,但由于以下问题,我强烈建议您尽可能避免这种情况:
There are many modern free (as in beer , as well as in speech ) compilers and IDEs you can use in place of Turbo C++.有许多现代免费(如啤酒和语音)编译器和 IDE 可以代替 Turbo C++ 使用。 Some of these include:其中一些包括:
Regrettably, some schools/teachers appear to force students to use Turbo C++ even in this day and age.遗憾的是,即使在这个时代,一些学校/教师似乎也强迫学生使用 Turbo C++。 Unfortunately this is not something this community can fix.不幸的是,这不是该社区可以解决的问题。 If you find yourself in this situation, prepare to not being able to get much outside help.如果您发现自己处于这种情况,请准备好无法获得太多外部帮助。
"Turbo C++" can mean numerous compilers. “Turbo C++”可能意味着许多编译器。 When asking this question, it is important to include the version number.在问这个问题时,包含版本号很重要。
Borland Turbo C++ up to version 3.1 were pure MS DOS compilers in the classic blue background IDE. Borland Turbo C++ 到 3.1 版是经典蓝色背景 IDE 中的纯 MS DOS 编译器。 These were released roughly somewhere between 1989 to 1992, long before C++ had become standardized, which happened in the year 1998. And so they used a pre-standard dialect of C++.它们大约在 1989 年到 1992 年之间发布,早在 C++ 标准化之前很久,这发生在 1998 年。所以他们使用了 C++ 的前标准方言。
Most notably they used the #include <iostream.h>
syntax rather than the standard #include <iostream>
, but also didn't cover a whole lot of C++ features such as namespaces, templates etc. The template library STL was not part of the standard yet, so everything related to that library was pretty different from what later became standard.最值得注意的是,他们使用了#include <iostream.h>
语法,而不是标准的#include <iostream>
,但也没有涵盖很多 C++ 功能,例如命名空间、模板等。模板库 STL 不属于标准,因此与该库相关的所有内容都与后来成为标准的内容大不相同。
Later in the 90s, Borland released several DOS/Windows compilers with better conformance.在 90 年代后期,Borland 发布了几个具有更好一致性的 DOS/Windows 编译器。 Up to version 5 somewhere they still struggled with complete conformance to C++98, although these Windows versions were fairly close to it.直到版本 5,他们仍然在努力完全符合 C++98,尽管这些 Windows 版本相当接近它。
In the late 90s, they dropped the name "Turbo C++" in favour for Borland C++ Builder, which was not just an IDE but a complete RAD tool based on Delphi.在 90 年代后期,他们放弃了“Turbo C++”的名称,转而使用 Borland C++ Builder,它不仅是一个 IDE,而且是一个基于 Delphi 的完整 RAD 工具。 These compilers were fully compliant with C++98 and later C++03.这些编译器完全符合 C++98 和更高版本的 C++03。
Around 2005, Borland dropped compilers as part of their product line. 2005 年左右,Borland 放弃了编译器作为其产品线的一部分。 The compilers became "Codegear", which later became Embarcadero.编译器变成了“Codegear”,后来变成了 Embarcadero。 Somewhere around then, they released a free version of Borland Builder that they named "Turbo C++".大约在那时,他们发布了一个免费版本的 Borland Builder,他们将其命名为“Turbo C++”。 This version was fully conforming to C++03.此版本完全符合 C++03。
Nowadays these compilers are called Embarcadero C++ Builder.如今,这些编译器被称为 Embarcadero C++ Builder。 I believe they currently support up to C++11 with some C++14.我相信他们目前支持 C++11 和一些 C++14。 More info here. 更多信息在这里。
Needless to say, as a student you should not use anything but modern compilers.毋庸置疑,作为学生,除了现代编译器外,您不应该使用任何东西。 Using MS DOS compilers from 1991 when learning C++ in the year 2018 is simply madness.在 2018 年学习 C++ 时使用 1991 年的 MS DOS 编译器简直是疯了。 Not only is it counter-productive, it is directly harmful and will make you a bad C++ programmer.它不仅适得其反,而且直接有害,会使您成为一个糟糕的 C++ 程序员。 If your school is forcing you to use Turbo C++ 3.1 or older, then your school is bad and your teachers are severely incompetent.如果你的学校强迫你使用 Turbo C++ 3.1 或更高版本,那么你的学校很糟糕,你的老师也很不称职。 Please link this post to them and their principal.请将这篇文章链接到他们和他们的校长。
Turbo C++ is a very old compiler and it is a little bit different from the GNU C++ compiler. Turbo C++ 是一个非常古老的编译器,它与 GNU C++ 编译器有点不同。 The code you shared will work perfectly with the GNU compiler but to run it with Turbo C++ you need to make a few changes:您共享的代码将与 GNU 编译器完美配合,但要使用 Turbo C++ 运行它,您需要进行一些更改:
1. Change the name of header file from iostream
to iostream.h
1.将头文件名由iostream
改为iostream.h
2. And remove the line "using namespace std" It is not required in Turbo C++. 2. 并删除“using namespace std”这一行。Turbo C++ 中不需要。 Here is the modified code:这是修改后的代码:
#include <iostream.h>
int main()
{
cout << "Hello, World!";
return 0;
}
It won't work like this... You've to make some changes to make it work... It should be: 它不会像这样工作...您必须进行一些更改才能使其工作...应该是:
#include <iostream.h> // Not #include <iostream>
#include <conio.h> // for clrscr() and getch() function
int main() {
clrscr(); // for clearing screen
cout << "Hello World!!" << endl;
getch(); // for stop screen from closing until user presses a key
return 0;
}
It should make it compatible to run in Turbo C++ 它应该兼容在Turbo C ++中运行
Hope it helps! 希望能帮助到你! :-) :-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.