作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试图在定义类之前创建类对象。这是示例。
class A;
class B;
class A{
public:
void fun(B obj){
}
};
class B{ };
int main(){
return 0;
}
这是错误列表:
In member function 'void A::fun(B)':
6 13 [Error] 'obj' has incomplete type
2 7 [Error] forward declaration of 'class B'
有什么解决办法吗?我已经搜索过了,但没能解决这个问题..提前致谢。
最佳答案
在 B
完全声明之后,提供 A::fun()
的外联定义:
#include <iostream>
class A;
class B;
class A {
public:
void fun(B obj);
};
class B{ };
void A::fun(B obj) {}
int main() {
return 0;
}
定义通常会放入 .cpp 文件中,您可以在其中包含 B.h
以获得完整的声明。
关于c++ - 如何在 C++ 中使用类原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053699/
我是一名优秀的程序员,十分优秀!