C/C++ 中不允许嵌套注释。但是,当您尝试将一对注释放入另一对注释时,以下错误意味着什么?

dex*_*ter -3 c c++ comments

考虑 Lippman 的“C++ Primer”中的以下代码,

#include <iostream>

/*
* comment pairs /*   */ cannot nest.
* "cannot nest" is considered source code,
* as is the rest of the program
*/

int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译时,

 $cl -EHsc .\Program.cc
 Microsoft (R) C/C++ Optimizing Compiler Version 19.30.30706 for x86
 Copyright (C) Microsoft Corporation.  All rights reserved.
 
 Program.cc
 .\Program.cc(4): error C4430: missing type specifier - int assumed. Note: C++ does not 
 support default-int
 .\Program.cc(4): error C2146: syntax error: missing ';' before identifier 'nest'
 .\Program.cc(7): warning C4138: '*/' found outside of comment
 .\Program.cc(10): error C2143: syntax error: missing ';' before '{'
 .\Program.cc(10): error C2447: '{': missing function header (old-style formal list?)
Run Code Online (Sandbox Code Playgroud)

问题1:书中上述代码中的““不能嵌套”被视为源代码,程序的其余部分也是如此”是什么意思?

问题 2:嵌套注释时会发生什么?这些相当混乱的错误产生的意义是什么?

BoP*_*BoP 5

注释以 a 开头/*,以 结束*/。所以在例子中,注释是

/*
* comment pairs /*   */
Run Code Online (Sandbox Code Playgroud)

事实上,/*评论中有第二个内容并不会“重新启动”它。它仍然以*/.

然后编译器尝试将其解释cannot nest.为源代码。最好的猜测似乎是这int cannot可能是一个变量声明,但缺少了int。然后nest也有点错误,而且错误还在继续......