本文介绍了“在文件结尾处没有换行符”编译器警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一些C ++编译器中出现以下警告的原因是什么?

What is the reason for the following warning in some C++ compilers?

为什么在源文件/头文件末尾应该有空行?

Why should I have an empty line at the end of a source/header file?

推荐答案

想想如果没有换行符,可能会发生的一些问题。根据ANSI标准,开头的文件的 #include 将文件完全插入到文件的前面,并且不插入新行, code> #include< foo.h> 后的文件内容。因此,如果包括在解析器末尾没有换行符的文件,将看起来好像 foo.h 的最后一行与第一行在同一行上 foo.cpp 。如果foo.h的最后一行是没有新行的注释怎么办?现在 foo.cpp 的第一行被注释掉了。

Think of some of the problems that can occur if there is no newline. According to the ANSI standard the #include of a file at the beginning inserts the file exactly as it is to the front of the file and does not insert the new line after the #include <foo.h> after the contents of the file. So if you include a file with no newline at the end to the parser it will be viewed as if the last line of foo.h is on the same line as the first line of foo.cpp. What if the last line of foo.h was a comment without a new line? Now the first line of foo.cpp is commented out. These are just a couple of examples of the types of problems that can creep up.

编辑:只想指定任何感兴趣的人到詹姆斯下面回答。虽然上面的答案对于C仍然是正确的,新的C ++标准(C ++ 11)已经改变,如果使用C ++和C ++ 11一致的编译器,不应该再发出这个警告。

Edit: Just wanted to point any interested parties to James' answer below. While The above answer is still correct for C the new C++ standard (C++11) has been changed so that this warning should no longer be issued if using C++ and a C++11 conformant compiler.

通过James的post从C ++ 11标准:

From C++11 standard via James' post:

这篇关于“在文件结尾处没有换行符”编译器警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:10