C ++不完整类型是不允许类内部使用的? [英] C++ incomplete type is not allowed class inner use?

查看:105
本文介绍了C ++不完整类型是不允许类内部使用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个标头 Room.h ,定义如下:

I have a header Room.h defined as follows:

#ifndef ROOM_H
#define ROOM_H

class Room
{
public:
    Room();

private:
    Room north;
    Room south;
    Room east; 
    Room west;
};

#endif

但是我得到错误:每个 Room 变量都不允许使用不完整的类型

But I get Error: incomplete type is not allowed for each of the Room variables. Is there a fundamental flaw in this kind of design?

推荐答案

简短答案



使用指针。

Short Answer

Use pointer.

private:
Room* north;
...



长答案



C ++编译器需要知道Class的大小。 错误:类型不完整,因为无法计算大小。

Long Answer

C++ compiler need know size of Class. Error: incomplete type is not allowed, because cannot compute size.

使用指针。因为编译器可以计算指针的大小。

Use pointer. Because compiler can compute size of pointer.

PS:我的英语不太好

PS : I'm not good at english

这篇关于C ++不完整类型是不允许类内部使用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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