在c ++中关联两个类 [英] relating two classes in c++

查看:70
本文介绍了在c ++中关联两个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以下结构。

我想在服务器类中使用客户端类对象。但编译器Dose不允许我。

所有其他变量都应该被假定为声明。



I Desire the following structure.
i want to use client class object in server class. But compiler Dose not allow me.
all othere variables should be assumed declared.

class client;
class Server
{
     client obj;
           public:
char* like(){
     return chaa;}
int kiss(){
     return inn++;}

     };
class client{
    int k,l;
     public:
client(){k=0;l=0;}
     };



谢谢。


Thank you.

推荐答案

当使用前向引用时,例如

When using forward references like
class client;



你不能用它作为成员类本身未定义的另一个类。



在你的情况下,只需在服务器客户端类$ c> class(移到顶部)。



或者更改客户端成员的类型您的服务器类到指针:


you can not use it as member of another class while the class itself is not defined.

In your case just define the client class before the Server class (move it to the top).

Alternatively change the type of the client member in your Server class to a pointer:

class Server
{
     client *obj;
    // ...
}



这将编译。


This would compile.


这篇关于在c ++中关联两个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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