为什么我不能多声明一个类 [英] Why can't I multi-declare a class

查看:115
本文介绍了为什么我不能多声明一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以这样做

  extern int i; 
extern int i;

但我不能对类进行同样的操作

  A类{
..
}
A类{
..
}



>解决方案

以下是声明

  extern int i; 
A类;

接下来两个是定义

  int i; 
class A {...}

规则是:




  • 定义也是一个声明。

  • 您必须先查看项目的声明,才能使用它。

  • 重新声明正确(必须相同)。

  • 重新定义是错误(单一定义规则)。


I can do this

extern int i;
extern int i;

But I can't do the same with a class

class A {
..
}
class A {
..
}

While in both cases no memory is being allocated.

解决方案

The following are declarations:

extern int i;
class A;

And the next two are definitions:

int i;
class A { ... }

The rules are:

  • a definition is also a declaration.
  • you have to have 'seen' a declaration of an item before you can use it.
  • re-declaration is OK (must be identical).
  • re-definition is an error (the One Definition Rule).

这篇关于为什么我不能多声明一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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