C++ 为什么这个程序被三个C++编译器错误拒绝了?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5508110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:19:32  来源:igfitidea点击:

Why is this program erroneously rejected by three C++ compilers?

c++visual-c++compiler-errorsclang

提问by James McNellis

I am having some difficulty compiling a C++ program that I've written.

我在编译我编写的 C++ 程序时遇到了一些困难。

This program is very simple and, to the best of my knowledge, conforms to all the rules set forth in the C++ Standard. I've read over the entirety of ISO/IEC 14882:2003 twice to be sure.

这个程序非常简单,据我所知,它符合 C++ 标准中规定的所有规则。为了确定,我已经通读了 ISO/IEC 14882:2003 两次。

The program is as follows:

程序如下:

enter image description here

在此处输入图片说明

Here is the output I received when trying to compile this program with Visual C++ 2010:

这是我在尝试使用 Visual C++ 2010 编译此程序时收到的输出:

c:\dev>cl /nologo helloworld.png
cl : Command line warning D9024 : unrecognized source file type 'helloworld.png', object file assumed
helloworld.png : fatal error LNK1107: invalid or corrupt file: cannot read at 0x5172

Dismayed, I tried g++ 4.5.2, but it was equally unhelpful:

沮丧,我尝试了 g++ 4.5.2,但它同样没有帮助:

c:\dev>g++ helloworld.png
helloworld.png: file not recognized: File format not recognized
collect2: ld returned 1 exit status

I figured that Clang (version 3.0 trunk 127530) must work, since it is so highly praised for its standards conformance. Unfortunately, it didn't even give me one of its pretty, highlighted error messages:

我认为 Clang(版本 3.0 主干 127530)必须工作,因为它因其标准一致性而受到高度赞扬。不幸的是,它甚至没有给我一个漂亮的、突出显示的错误消息:

c:\dev>clang++ helloworld.png
helloworld.png: file not recognized: File format not recognized
collect2: ld returned 1 exit status
clang++: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)

To be honest, I don't really know what any of these error message mean.

老实说,我真的不知道这些错误消息是什么意思。

Many other C++ programs have source files with a .cppextension, so I thought perhaps I needed to rename my file. I changed its name to helloworld.cpp, but that didn't help. I think there is a very serious bug in Clang because when I tried using it to compile the renamed program, it flipped out, printed "84 warnings and 20 errors generated." and made my computer beep a lot!

许多其他 C++ 程序都有带有.cpp扩展名的源文件,所以我想也许我需要重命名我的文件。我将其名称更改为helloworld.cpp,但这没有帮助。我认为 Clang 有一个非常严重的错误,因为当我尝试使用它来编译重命名的程序时,它会翻转,打印出“84 个警告和 20 个错误生成”。并让我的电脑发出很大的哔哔声!

What have I done wrong here? Have I missed some critical part of the C++ Standard? Or are all three compilers really just so broken that they can't compile this simple program?

我在这里做错了什么?我是否错过了 C++ 标准的一些关键部分?或者这三个编译器真的都坏到无法编译这个简单的程序了吗?

采纳答案by GManNickG

In the standard, §2.1/1 specifies:

在标准中,§2.1/1 规定:

Physical source file characters are mapped, in an implementation-defined manner, to the basic source character set (introducing new-line characters for end-of-line indicators) if necessary.

如有必要,物理源文件字符以实现定义的方式映射到基本源字符集(为行尾指示符引入换行符)。

Your compiler doesn't support that format (aka cannot map it to the basic source character set), so it cannot move into further processing stages, hence the error. It is entirely possible that your compiler support a mapping from image to basic source character set, but is not required to.

您的编译器不支持该格式(又名无法将其映射到基本源字符集),因此它无法进入进一步的处理阶段,因此会出现错误。您的编译器完全有可能支持从图像到基本源字符集的映射,但不是必需的。

Since this mapping is implementation-defined, you'll need to look at your implementations documentation to see the file formats it supports. Typically, every major compiler vendor supports (canonically defined) text files: any file produced by a text editor, typically a series of characters.

由于此映射是实现定义的,您需要查看您的实现文档以查看它支持的文件格式。通常,每个主要的编译器供应商都支持(规范定义的)文本文件:由文本编辑器生成的任何文件,通常是一系列字符。



Note that the C++ standard is based off the C standard (§1.1/2), and the C(99) standard says, in §1.2:

请注意,C++ 标准基于 C 标准 (§1.1/2),而 C(99) 标准在 §1.2 中说:

This International Standard does not specify
— the mechanism by which C programs are transformed for use by a data-processing system;
— the mechanism by which C programs are invoked for use by a data-processing system;
— the mechanism by which input data are transformed for use by a C program;

本国际标准没有规定
——转换 C 程序以供数据处理系统使用的机制;
——调用C程序供数据处理系统使用的机制;
— 输入数据被转换以供 C 程序使用的机制;

So, again, the treatment of source files is something you need to find in your compilers documentation.

因此,同样,源文件的处理是您需要在编译器文档中找到的内容。

回答by Sven

Originally from Overv@ reddit.

最初来自Overv@ reddit

回答by Benoit

Try this way:

试试这个方法:

enter image description here

在此处输入图片说明

回答by Bala R

Your <and >, (and ), {and }don't seem to match very well; Try drawing them better.

您的<and >, (and ), {and }似乎不太匹配;试着把它们画得更好。

回答by sje397

You could try the following python script. Note that you need to install PILand pytesser.

您可以尝试以下 python 脚本。请注意,您需要安装PILpytesser

from pytesser import *
image = Image.open('helloworld.png')  # Open image object using PIL
print image_to_string(image)     # Run tesseract.exe on image

To use it, do:

要使用它,请执行以下操作:

python script.py > helloworld.cpp; g++ helloworld.cpp

回答by bl00dshooter

You forgot to use Comic Sans as a font, that's why its erroring.

您忘记使用 Comic Sans 作为字体,这就是它出错的原因。

回答by Michael Burr

I can't see a new-line after that last brace.

在最后一个大括号之后我看不到换行符。

As you know: "If a source file that is not empty does not end in a new-line character, ... the behavior is undefined".

如您所知:“如果非空的源文件不以换行符结尾,......行为未定义”。

回答by Jerry Asher

This program is valid -- I can find no errors.

这个程序是有效的——我找不到任何错误。

My guess is you have a virus on your machine. It would be best if you reformat your drive, and reinstall the operating system.

我的猜测是你的机器上有病毒。最好重新格式化驱动器并重新安装操作系统。

Let us know how that works out, or if you need help with the reinstall.

让我们知道它是如何工作的,或者如果您需要重新安装的帮助。

I hate viruses.

我讨厌病毒。

回答by the Tin Man

I've found it helps to not write my code on my monitor's glass with a magic marker, even though it looks nice when its really black. The screen fills up too fast and then the people who give me a clean monitor call me names each week.

我发现不要用魔术笔在显示器的玻璃上写我的代码会有所帮助,即使它真的很黑时看起来很好。屏幕填得太快,然后给我一个干净的显示器的人每周都给我打电话。

A couple of my employees (I'm a manager) are chipping in to buy me one of those red pad computers with the knobs. They said that I won't need markers and I can clean the screen myself when it's full but I have to be careful shaking it. I supposed it's delicate that way.

我的几个员工(我是一名经理)正在凑钱给我买一台带旋钮的红色笔记本电脑。他们说我不需要标记,我可以在屏幕满的时候自己清洁屏幕,但我必须小心摇晃它。我想这样很微妙。

That's why I hire the smart people.

这就是我雇用聪明人的原因。

回答by helloworld922

File format not recognizedYou need to properly format your file. That means using the right colors and fonts for your code. See the specific documentations for each compiler as these colors vary between compiler ;)

File format not recognized您需要正确格式化您的文件。这意味着为您的代码使用正确的颜色和字体。请参阅每个编译器的特定文档,因为这些颜色因编译器而异;)