为什么我必须在C ++的类声明的末尾添加一个分号? [英] Why must I put a semicolon at the end of class declaration in C++?

查看:395
本文介绍了为什么我必须在C ++的类声明的末尾添加一个分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++类声明中:

  class Thing 
{
...
};

为什么必须包含分号?

解决方案

完整的语法基本上是



class NAME {constituents}实例;



其中constituents是类元素和方法的序列,instances是类(即对象)的实例的逗号分隔列表。



示例:

  class FOO {
int bar;
int baz;
} waldo;

声明类FOO和对象waldo。



实例序列可能为空,在这种情况下,您只需要

  class FOO {
int酒吧;
int baz;
};

你必须把分号放在那里,以便编译器知道你是否声明了任何实例。 / p>

这是一个C兼容性的东西。


In a C++ class declaration:

class Thing
{
    ...
};

why must I include the semicolon?

解决方案

The full syntax is, essentially,

class NAME { constituents } instances ;

where "constituents" is the sequence of class elements and methods, and "instances" is a comma-separated list of instances of the class (i.e., objects).

Example:

class FOO {
  int bar;
  int baz;
} waldo;

declares both the class FOO and an object waldo.

The instance sequence may be empty, in which case you would have just

class FOO {
  int bar;
  int baz;
};

You have to put the semicolon there so the compiler will know whether you declared any instances or not.

This is a C compatibility thing.

这篇关于为什么我必须在C ++的类声明的末尾添加一个分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆