C创建“包装器”
发布时间:2020-12-16 09:52:11 所属栏目:百科 来源:网络整理
导读:在下面更新 我正在尝试创建此包装器以包含指向所有其他类的指针.我遇到了这个问题(例子): main.cpp中 struct wrap { Game* game; Player* player; Map* map;}; game.h class Game { private: wrap* info;} 有没有解决方法,包装需要游戏,游戏需要包装. (我知
在下面更新
我正在尝试创建此包装器以包含指向所有其他类的指针.我遇到了这个问题(例子): main.cpp中 struct wrap { Game* game; Player* player; Map* map; }; game.h class Game { private: wrap* info; } 有没有解决方法,包装需要游戏,游戏需要包装. (我知道包装类[此案例结构]不是最佳实践,但我在其他类中经常需要该信息.) 更新 items.h // top struct CoreInfo; void Items::test() { struct CoreInfo* b; //b->testing = 4; } (结构CoreInfo包含一个变量“int testing.”,我无法弄清楚如何访问items类中的任何内容,正常错误:7请求’b’中的成员’testing’,这是非类型的’CoreInfo’ *” 解决方法
只需向前声明wrap结构,如下所示:
main.cpp中 #include "game.h" struct wrap { Game* game; Player* player; Map* map; }; game.h struct wrap; class Game { private: struct wrap* info; } 编辑: 问题是你没有利用编译单元在声明和定义之间进行分离.如果你在编译单元(items.cpp)中定义你的类及其成员,在标题items.h中声明它,你就没有问题. 让我们举一个例子来说明这一点: foo.h中 #include "bar.h" class A { B b_instance; void do_something(int i,int j); } Foo.cpp中 #include "foo.h" int A::do_something(int i,int j) { return i+j; } bar.h class B { A a_instance; void use_a(); } bar.cpp #include "foo.h" // which includes bar.h as well void B::use_a() { int k = a_instance.do_something(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |