简体   繁体   English

为什么一个简单的“Hello World”风格的程序不能用 Turbo C++ 编译?

[英]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++ 学习编程,但由于以下问题,我强烈建议您尽可能避免这种情况:

  1. You will be learning a language that is somewhat similar to a popular language used in the industry, but is very different nevertheless, for no good reason.您将学习一种与行业中使用的流行语言有些相似但又非常不同的语言,没有充分的理由。 If you plan to write C++ for real software development, you will have to re-learn much.如果您打算为真正的软件开发编写 C++,您将不得不重新学习很多东西。 It is much easier to learn modern C++ right away.立即学习现代 C++ 要容易得多。
  2. There's no extant literature about Turbo C++.没有关于 Turbo C++ 的现存文献。 Nearly 100% of C++ material you will find on the internet or in the books is not directly applicable to Turbo C++ out of the box.您可以在互联网或书中找到的几乎 100% 的 C++ 材料都不能直接适用于开箱即用的 Turbo C++。 Some will need only minor adaptation, while other material is completely unusable.有些只需要很小的调整,而其他材料则完全无法使用。 Pretty much the only source of help immediately available to you is the built-in Turbo C++ help.几乎可以立即获得的唯一帮助来源是内置的 Turbo C++ 帮助。
  3. Few people remember Turbo C++.很少有人记得 Turbo C++。 When asking questions on forums, always specify that you are using a pre-ANSI dialect in order to filter out responses geared towards the modern version of the language.在论坛上提问时,请始终指定您使用的是 ANSI 之前的方言,以过滤出针对该语言现代版本的回复。 You will probably get a bunch of comments suggesting you to stop immediately and switch to a modern compiler with every question you ask.您可能会收到一堆评论,建议您立即停止并针对您提出的每个问题切换到现代编译器。

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:其中一些包括:

  1. Visual C++ Community Edition is an IDE and a compiler from Microsoft Visual C++ Community Edition是来自 Microsoft 的 IDE 和编译器
  2. Code::Blocks is a lightweight IDE. Code::Blocks是一个轻量级的 IDE。 On Windows it ships with a somewhat outdated compiler, but you can install a more modern compiler yourself在 Windows 上,它附带了一个有点过时的编译器,但您可以自己安装一个更现代的编译器
  3. Eclipse CDT is a powerful cross-platform IDE. Eclipse CDT是一个强大的跨平台IDE。 It doesn't ship with its own compiler so you need to install a separate compiler.它不附带自己的编译器,因此您需要安装单独的编译器。 On Windows, use eg MinGW .在 Windows 上,使用例如MinGW
  4. Many more 还有很多
  5. In addition, there are many online compilers such as http://ideone.com , https://www.onlinegdb.com/ and http://coliru.stacked-crooked.com/ , plus many more (these are mostly good for trying out ideas and writing very small programs).此外,还有很多在线编译器,例如http://ideone.comhttps://www.onlinegdb.com/http://coliru.stacked-crooked.com/ ,以及更多(这些大多是好的用于尝试想法和编写非常小的程序)。
  6. Both Clang/LLVM and GCC are free software compilers supporting recent versions of C++. Clang/LLVMGCC都是支持 C++ 最新版本的免费软件编译器。

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.

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