gpt4 book ai didi

c++ - 类成员初始化C++

转载 作者:行者123 更新时间:2023-11-28 05:05:12 28 4
gpt4 key购买 nike

我想在创建游戏对象时初始化转换变量,但出了点问题。编译器说:

C3646   'transform' : unknown override specifier    (line 4)
C4430 missing type specifier - int assumed. Note: C++ does not support default-int (line 4)
C3861 'Transform': identifier not found (line 5)
C2614 'GameObject' : illegal member initialization: 'transform' is not a base or member (line 5)

1. class GameObject
2. {
3. public:
4. Transform transform;
5. GameObject() : transform(Transform()) {}
6. };
7.
8. class Transform
9. {
10. public:
11. Vector3 position;
12. Vector3 rotation;
13. Vector3 dimension;
14.
15. Transform()
16. {
17. position = Vector3();
18. rotation = Vector3();
19. dimension = Vector3();
20. }
21. }

在 main.cpp 中我调用:

GameObject theGameObject = GameObject();

我做错了什么?

最佳答案

在你的 GameObject 类中你有一个 Transform 对象 Transform transform; 但编译器没有看到 Transform 类,因此它不知道它的大小,甚至不知道它是什么。您应该先定义 Transform,然后再定义 GameObject,而不是先定义 GameObject,再定义 Transform。您可能也想在单独的头文件中分隔这些类。


旁注:您也可以只执行 GameObject theGameObject;

关于c++ - 类成员初始化C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44998654/

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