有可能会碰到两个类之间的相互调用的问题,例如:定义了类A和类B,A中使用了B定义的类型,B中也使用了A定义的类型

class A {
B b;
}; class B {
A a;
};

  编译器在声明A的时候,由于B的声明在A的下面所以出现编译错误

那么,在定义时如果要相互调用肯定会需要相互包含头文件,如果仅仅只是在各自的头文件中包含对方的头文件,是通不过编译的:

//a.h
#include"b.h"
class A {
public:
B xianghuInA;
}; //b.h
#include"a.h"
class B {
public:
A xianghuInB;
};

怎么办?一般的做法是

1)class B采用前置声明的方式声明class A

2)在class B中只能声明class A类型的指针,因为定义指针时不需要对那个类的定义可见

3)在class A的头文件中包含class B 的头文件

//a.h
#pragma once
#include"b.h" //一定要有
class A {
public:
B xianghuInA;
int a = ;
void funA();
};
//b.h
#pragma once
#include<iostream> class A;
class B {
public:
int b = ;
A* xianghuInB = NULL; //一定要是指针
void funB();
~B();
};
//a.cpp
#include"a.h"
#include<iostream>
using namespace std; void A::funA()
{
cout << xianghuInA.b << endl;
}
//b.cpp
#include"b.h"
#include<iostream>
#include <typeinfo>
using namespace std; void B::funB()
{
cout << typeid(xianghuInB).name() << endl;
} B::~B() {
delete xianghuInB;
}
//main.cpp
#include<iostream>
#include"a.h"
#include"b.h"
using namespace std; int main()
{
A qq;
qq.funA();
B hh;
hh.funB();
return ;
}

运行结果:

或者:

//main.cpp
class B;
class A {
B* b;
}; class B {
A a;
};

c++两个类相互调用的更多相关文章

  1. Cocos Creator两个类相互引用(调用)

    如果两个类相互引用,脚本加载阶段就会出现循环引用,循环引用将导致脚本加载出错:///////////Game.jsvar Item = require("Item");var Ga ...

  2. 多态时最好将基类的析构函数设为virtual、 C++中两个类相互包含引用问题 (转载)

    多态:http://blog.csdn.net/tmljs1988/article/details/8146521 C++中两个类相互包含引用问题:http://blog.csdn.net/leo11 ...

  3. c++中两个类相互包含引用的相关问题

    在构造自己的类时,可能会遇到两个类相互引用的问题. 例如: class A { int i; B b; }; class B { int i; A a; }; 在这种情况下,这样就会出现一个死循环a. ...

  4. 谈谈java中静态变量与静态方法在有继承关系的两个类中调用

    谈谈java中静态变量与静态方法在有继承关系的两个类中调用 学习的中如果遇到不明白或者不清楚的的时候,就是自己做些测试,自己去试试,这次我就做一个关于静态变量和静态方法在有继承关系的两个类中的问题测试 ...

  5. C++两个类相互包含引用的问题

    在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 class A { B b; } class B { A* a; } 请 ...

  6. C++中两个类相互包含引用问题

    在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 class A { int i; B b; } class B { in ...

  7. C++中两个类相互include的问题

    在构造自己的类时,有可能会碰到两个类之间的相互引用问题,例如:定义了类A类B,A中使用了B定义的类型,B中也使用了A定义的类型 例如: Cup.h #ifndef CUP_H #define CUP_ ...

  8. java中各个类相互调用资源的原理

       当我们要进行跨类的调用/使用的时候,比如当前类调用另一个类中的变量或方法时, 这时需要一定的条件,如果那些将要被调用的变量或方法是static(静态)变量,也叫类变 量,那么可以通过类名调用,相 ...

  9. C++两个类相互引用错误留影

    java中类相互引用很方便,c++中有编译顺序的问题 c++声明作用,告诉编译器,我声明的东西你要是找不到,那就跳过去吧,等全编译完你就可以找到了. C1.h #pragma once #includ ...

随机推荐

  1. Python直接改变实例化对象的列表属性的值 导致在flask中接口多次请求报错

    错误原理实例如下: class One(): list = [1, 2, 3] @classmethod def get_copy_list(cls): # copy一份list,这样对list的改变 ...

  2. easy ui 关闭选项卡

    var tab = window.parent.getCurrentTab(); var tabs = window.parent.getTabs(); var index = tabs.tabs(& ...

  3. python下彻底解决浏览器多标签打开与切换问题

    #coding:utf-8#Right_key_click and Tad switch#by dengpeiyou date:2018-7-7from selenium import webdriv ...

  4. This iPhone 6s is running iOS 11.3.1 (15E302), which may not be supported by this version of Xcode.

    This iPhone 6s is running iOS 11.3.1 (15E302), which may not be supported by this version of Xcode.

  5. Android Studio2.0 教程从入门到精通Windows版 - 提高篇

    第二篇我们开发了一个Hello World应用,并介绍Android Sutdio的界面和如何调试应用,接下来将介绍一些常用的快捷键和必备插件. 常用快捷键 代码跳转 描述:跳转是为了方便代码位置的定 ...

  6. JAVA四则运算(读写文件)

    完成时间:17:10 package 四则运算试题; import java.io.BufferedReader; import java.io.PrintStream; import java.ut ...

  7. Linux - 操作系统

    操作系统(科普章节) 目标 了解操作系统及作用 1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自 ...

  8. Why is it called “armature” instead of “skeleton”? or perhaps “rig”?

    Great question, I’ve always assumed armature/skeleton to be the same thing, here’s a quote from an a ...

  9. SQL Server 定价及授权方式

    https://www.microsoft.com/zh-cn/sql-server/sql-server-2017-pricing http://www.360doc.com/content/15/ ...

  10. [leetcode]24. Swap Nodes in Pairs交换节点对

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...