简体   繁体   English

如何定义两个相互依赖的类,最好是在同一个头文件中?

[英]How can I define two inter dependant classes, preferably in the same header file?

So, I want something like: 所以,我想要的东西:

class A{
B member;
};

class B{
A function();
};

No matter in which order I declare them, I get an incomplete type error (and I pretty much understand why). 无论我以什么顺序声明它们,我都会得到一个不完整的类型错误(我非常理解为什么)。 How can I solve this? 我怎么解决这个问题? I don't want to use pointers or to have the function defined outside the B class. 我不想使用指针或在B类之外定义函数。 Also, declaring them before as 另外,在之前声明它们

class A; class B;

doesn't seem to work either. 似乎也没有用。

No need for class definition when declare a function. 声明函数时不需要类定义。

class A;
class B{
A function();
};
class A{
B member;
};

This order will work: 此订单将起作用:

class A;

class B {
  A function();
};

class A {
  B member;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从同一个头文件中定义2个类,而另一个类依赖于另一个类? - How can I define 2 classes from the same header file, while one class depends on the other? 在两个不同的类中包含相同的头文件 - Including the same header file in two different classes 如何在单独的头文件的结构中定义char *数组? - How can i define an array of char* in a struct in a separate header file? 我如何定义一个运算符重载函数来比较来自两个不同类的两个对象? - how can i define an operator overloading function to compare two objects from two different classes? 如何定义两个依赖类? - How to define two depend classes? 在 C 或 C++ 中,如何防止 header 文件中先前的#define 影响另一个 Z099FB9953406FC 文件包含的 EEFC 文件? - in C or C++, how can I prevent previous #define in a header file from affecting another header file later included? 如何使用条件编译从其他文件访问 header 文件中的定义宏? - How can I access the define macro in the header file from other files with Conditional Compilation? 我可以在 class header 文件中定义一个 class 的 const static 实例吗 - Can I define a const static instance of a class in the class header file C ++无法定义虚函数OUTSIDE类和头文件 - C++ can't define virtual function OUTSIDE classes and header file 如何链接(或解决)两个定义相同符号的第三方静态库? - How can I link with (or work around) two third-party static libraries that define the same symbols?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM