gpt4 book ai didi

c++ - "No newline at end of file"编译器警告

转载 作者:IT老高 更新时间:2023-10-28 11:28:42 30 4
gpt4 key购买 nike

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

No newline at end of file

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

最佳答案

想想如果没有换行符可能会出现的一些问题。根据 ANSI 标准 #include开头的文件将文件完全按原样插入到文件的前面,并且不在 #include <foo.h> 之后插入新行。在文件内容之后。因此,如果您在解析器的末尾包含一个没有换行符的文件,它将被视为 foo.h 的最后一行。与 foo.cpp 的第一行在同一行.如果 foo.h 的最后一行是没有新行的注释怎么办?现在 foo.cpp 的第一行被注释掉了。这些只是可能出现的问题类型的几个示例。


只是想向任何感兴趣的人指出詹姆斯在下面的回答。虽然上述答案对于 C 语言仍然正确,但新的 C++ 标准 (C++11) 已更改,因此如果使用 C++ 和符合 C++11 的编译器,则不应再发出此警告。

来自 C++11 标准,来自 James 的帖子:

A source file that is not empty and that does not end in a new-line character, or that ends in a new-line character immediately preceded by a backslash character before any such splicing takes place, shall be processed as if an additional new-line character were appended to the file (C++11 §2.2/1).

关于c++ - "No newline at end of file"编译器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72271/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com