美文网首页
C++ 注释的不正确嵌套

C++ 注释的不正确嵌套

作者: 顽强的猫尾草 | 来源:发表于2019-03-02 10:42 被阅读6次

用以下程序举例:

#include <iostream>
/*
* 注释对 /* */ 不能嵌套
* 否则后面的字会被认为是源码,像剩余程序一样处理
*/

int main() {
    return 0;
}

命名为 exa.cpp,然后编译这个程序。

# g++ exa.cpp
exa.cpp:4:17: error: stray ‘\344’ in program
 * 注释对/* */不能嵌套
                 ^
exa.cpp:4:17: error: stray ‘\270’ in program
exa.cpp:4:17: error: stray ‘\215’ in program
exa.cpp:4:17: error: stray ‘\350’ in program
exa.cpp:4:17: error: stray ‘\203’ in program
exa.cpp:4:17: error: stray ‘\275’ in program
exa.cpp:4:17: error: stray ‘\345’ in program
exa.cpp:4:17: error: stray ‘\265’ in program
exa.cpp:4:17: error: stray ‘\214’ in program
exa.cpp:4:17: error: stray ‘\345’ in program
exa.cpp:4:17: error: stray ‘\245’ in program
exa.cpp:4:17: error: stray ‘\227’ in program
exa.cpp:5:1: error: stray ‘\342’ in program
 * “不能嵌套”几个字会被认为是源码,像剩余程序一样处理
 ^
exa.cpp:5:1: error: stray ‘\200’ in program
exa.cpp:5:1: error: stray ‘\234’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\215’ in program
exa.cpp:5:1: error: stray ‘\350’ in program
exa.cpp:5:1: error: stray ‘\203’ in program
exa.cpp:5:1: error: stray ‘\275’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\265’ in program
exa.cpp:5:1: error: stray ‘\214’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\245’ in program
exa.cpp:5:1: error: stray ‘\227’ in program
exa.cpp:5:1: error: stray ‘\342’ in program
exa.cpp:5:1: error: stray ‘\200’ in program
exa.cpp:5:1: error: stray ‘\235’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\207’ in program
exa.cpp:5:1: error: stray ‘\240’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\252’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\255’ in program
exa.cpp:5:1: error: stray ‘\227’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\274’ in program
exa.cpp:5:1: error: stray ‘\232’ in program
exa.cpp:5:1: error: stray ‘\350’ in program
exa.cpp:5:1: error: stray ‘\242’ in program
exa.cpp:5:1: error: stray ‘\253’ in program
exa.cpp:5:1: error: stray ‘\350’ in program
exa.cpp:5:1: error: stray ‘\256’ in program
exa.cpp:5:1: error: stray ‘\244’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\272’ in program
exa.cpp:5:1: error: stray ‘\346’ in program
exa.cpp:5:1: error: stray ‘\230’ in program
exa.cpp:5:1: error: stray ‘\257’ in program
exa.cpp:5:1: error: stray ‘\346’ in program
exa.cpp:5:1: error: stray ‘\272’ in program
exa.cpp:5:1: error: stray ‘\220’ in program
exa.cpp:5:1: error: stray ‘\347’ in program
exa.cpp:5:1: error: stray ‘\240’ in program
exa.cpp:5:1: error: stray ‘\201’ in program
exa.cpp:5:1: error: stray ‘\357’ in program
exa.cpp:5:1: error: stray ‘\274’ in program
exa.cpp:5:1: error: stray ‘\214’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\203’ in program
exa.cpp:5:1: error: stray ‘\217’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\211’ in program
exa.cpp:5:1: error: stray ‘\251’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\275’ in program
exa.cpp:5:1: error: stray ‘\231’ in program
exa.cpp:5:1: error: stray ‘\347’ in program
exa.cpp:5:1: error: stray ‘\250’ in program
exa.cpp:5:1: error: stray ‘\213’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\272’ in program
exa.cpp:5:1: error: stray ‘\217’ in program
exa.cpp:5:1: error: stray ‘\344’ in program
exa.cpp:5:1: error: stray ‘\270’ in program
exa.cpp:5:1: error: stray ‘\200’ in program
exa.cpp:5:1: error: stray ‘\346’ in program
exa.cpp:5:1: error: stray ‘\240’ in program
exa.cpp:5:1: error: stray ‘\267’ in program
exa.cpp:5:1: error: stray ‘\345’ in program
exa.cpp:5:1: error: stray ‘\244’ in program
exa.cpp:5:1: error: stray ‘\204’ in program
exa.cpp:5:1: error: stray ‘\347’ in program
exa.cpp:5:1: error: stray ‘\220’ in program
exa.cpp:5:1: error: stray ‘\206’ in program
exa.cpp:6:2: error: expected unqualified-id before ‘/’ token
 */
  ^
exa.cpp:6:2: error: expected constructor, destructor, or type conversion before ‘/’ token

输出的报错...

如果不是已经知道了问题才看到报错,而是要从报错反推问题,实在会让人束手无策。

究其原因就是注释对 “/* */” 不可嵌套,“/*” 总会与后文遇到的第一个 “*/” 组成一对,“*/” 总会与前文最近的 “/*” 组成一对。

当然这是指每行都不存在分号的情况下,如果有分号,这一行不论遇到了什么都结束了。

几个例子:

cout << "/*";                  // 输出“/*”
cout << "*/";                  // 输出“*/”
cout << /* "*/" /* "/*" */;    // 输出“ /* ”




PS. 今天收到了简书送来的礼物,是一本宋词书,haha。那么以后只要有 idea 我就来更一波~

相关文章

  • C++ 注释的不正确嵌套

    用以下程序举例: 命名为 exa.cpp,然后编译这个程序。 输出的报错... 如果不是已经知道了问题才看到报错,...

  • Java 学习笔记_5

    1、多行注释可以嵌套单行注释。不可以嵌套多行注释. 2、System.out.println(1+2+"java"...

  • 3.Java基础复习

    1、三种注释 注意:多行注释可以嵌套单行注释,但是多行注释不能嵌套多行注释 2、Java中表示符 在程序中经常要定...

  • IOS(一)swift知识点速记

    注释 单行 // 多行 /* xxxx */ 注释嵌套/* xxx/* aaaa*/fff */...

  • Java学习第二天

    一:标识符 注释: (1)// 单行注释 (2)/* */ 多行注释 (该注释不能嵌套) (3)/** ...

  • 学习Java第一天

    单行注释: // 多行注释:/* 注释内容*/ 文档注释:/**注释内容*/ 各种注释无法嵌套使用 所有关键字都是...

  • NO.4 Java的基础语法

    1、注释: 用于解释说明程序的文字 , 分类格式:单行注释(//注释文字可以嵌套),多行注释(/* 注释文...

  • ★学习笔记:《iOS高级:Swift入门精讲①》第一节 Swif

    前言:本篇为视频学习笔记 注释★1// 单行注释★2/* 多行注释*/★3 /* 1 /* 多行注释嵌套...

  • 2018-02-04

    介绍 输出语句 常用输出语句 代码注释 //单行注释 /* 多行注释 */ 书写多行注释的时候注意不要发生嵌套 /...

  • Swift笔记<二十二>常见注释

    单行注释 当行注释以//作为标记 多行注释 以/*开始 */ 结束 可进行多行嵌套注释 文档注释 commmand...

网友评论

      本文标题:C++ 注释的不正确嵌套

      本文链接:https://www.haomeiwen.com/subject/lxqeuqtx.html