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

我已经开始为我的编程课学习 C + + 了,我下载了这个“ Hello World”程序:

#include <iostream>
using namespace std;


int main()
{
cout << "Hello, World!";
return 0;
}

但 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'

这个非常简单的程序出了什么问题? 我怎样才能纠正这些错误?

12309 次浏览

There's no problem with this program. (Except probably some stylistic issues — using namespace std is not recommended). The problem is with 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. 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. 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:

#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. 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:

  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. It is much easier to learn modern C++ right away.
  2. There's no extant literature about 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. 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.
  3. Few people remember 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. 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++. Some of these include:

  1. Visual C++ Community Edition is an IDE and a compiler from Microsoft
  2. Code::Blocks is a lightweight IDE. On Windows it ships with a somewhat outdated compiler, but you can install a more modern compiler yourself
  3. Eclipse CDT is a powerful cross-platform IDE. It doesn't ship with its own compiler so you need to install a separate compiler. On Windows, use e.g. 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).
  6. Both Clang/LLVM and GCC are free software compilers supporting recent versions of C++.

Regrettably, some schools/teachers appear to force students to use Turbo C++ even in this day and age. 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++ is a very old compiler and it is a little bit different from the GNU C++ compiler. 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:

1. Change the name of header file from iostream to iostream.h
2. And remove the line "using namespace std" It is not required in Turbo C++. Here is the modified code:

#include <iostream.h>


int main()
{
cout << "Hello, World!";
return 0;
}

"Turbo C++" can mean numerous compilers. 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. 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++.

    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.

  • Later in the 90s, Borland released several DOS/Windows compilers with better conformance. Up to version 5 somewhere they still struggled with complete conformance to C++98, although these Windows versions were fairly close to it.

  • 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. These compilers were fully compliant with C++98 and later C++03.

  • Around 2005, Borland dropped compilers as part of their product line. The compilers became "Codegear", which later became Embarcadero. Somewhere around then, they released a free version of Borland Builder that they named "Turbo C++". This version was fully conforming to C++03.

  • Nowadays these compilers are called Embarcadero C++ Builder. I believe they currently support up to C++11 with some 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. Not only is it counter-productive, it is directly harmful and will make you a bad C++ programmer. 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. Please link this post to them and their principal.