gpt4 book ai didi

c++ - 如何在 C++ 中使用类原型(prototype)

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:02 24 4
gpt4 key购买 nike

我试图在定义类之前创建类对象。这是示例。

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 以获得完整的声明。

此外,please don't using namespace std; .

关于c++ - 如何在 C++ 中使用类原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053699/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com