练习 1.7: 编译一个包含不正确的嵌套注视的程序,观察编译器返回的错误信息。
例如:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
/*
* This is a comment
/*
* This is another comment
*/
*/
return 0;
}
输出错误信息如下:
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build "/Users/benjaminliu/C++Primer/C++Primer 1.3节练习/cmake-build-debug" --target C__Primer_1_3 -- -j 2
Scanning dependencies of target C__Primer_1_3
[ 50%] Building CXX object CMakeFiles/C__Primer_1_3.dir/main.cpp.o
/Users/benjaminliu/C++Primer/C++Primer 1.3节练习/main.cpp:12:6: warning: '/*' within block comment [-Wcomment]
/*
^
/Users/benjaminliu/C++Primer/C++Primer 1.3节练习/main.cpp:15:7: error: expected expression
*/
^
/Users/benjaminliu/C++Primer/C++Primer 1.3节练习/main.cpp:17:5: error: expected expression
return 0;
^
1 warning and 2 errors generated.
make[3]: *** [CMakeFiles/C__Primer_1_3.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/C__Primer_1_3.dir/all] Error 2
make[1]: *** [CMakeFiles/C__Primer_1_3.dir/rule] Error 2
make: *** [C__Primer_1_3] Error 2
练习 1.8: 指出下列哪些输出语句时合法的(如果有的话):
std::cout << "/*";
std::cout << "*/";
std::cout << /* "*/" */;
std::cout << /* "/" /* "/" */;
预测编译这些语句会产生什么样的后果,实际编译这些语句来验证你的答案(编写一个小程序,每次将上述一条语句作为其主体),改正每个编译错误。
std::cout << "/*"; 正确,输出/*
std::cout << "*/";正确,输出*/
std::cout << /* "*/" */;错误,
warning: missing terminating '"' character [-Winvalid-pp-token]
std::cout << /* "*/" */;
error: expected expression
更正:std::cout << " /* */ */";
std::cout << /* "/" /* "/" */; 错误
更正:std::cout << " /* / /* / */ ";