本文介绍了循环依赖性/不完全类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 在C ++中,我有一个循环依赖/不完全类型的问题。情况如下: Stuffcollection.h #include Spritesheet.h; class Stuffcollection { public: void myfunc(Spritesheet * spritesheet); void myfuncTwo(); }; Stuffcollection.cpp void Stuffcollection :: myfunc(Spritesheet * spritesheet){ unsigned int myvar = 5 * spritesheet-> spritevar; } void myfunc2(){ // } $ b b Spritesheet.h #includeStuffcollection.h class Spritesheet { public : void init(); }; Spritesheet.cpp void Spritesheet :: init(){ Stuffcollection stuffme; myvar = stuffme.myfuncTwo(); } 我得到编译器错误 spritesheet尚未在Stuffcollection.h中声明(上面的中的第4行)。我理解这是由于循环依赖。 现在,如果我将 #includeSpritesheet.h更改为Forward 声明 Spritesheet; 在Stuffcollection.h中,我得到编译器错误无效使用不完全类型'struct Spritesheet' 类似地,如果我将 #includeStuffcollection.h更改为,则可以使用Stuffcollection.cpp(上面的第2行)。 c $ c> class Stuffcollection; 在Spritesheet.h中,我得到编译器错误 aggregate 'Stuffcollection stuffme'有不完整的类型, 在Spritesheet.cpp中的 解决方案 您应该包含 Spritesheet.h ,以解决这个问题? Stuffcollection.cpp 只使用头文件中的forward声明,而不是cpp文件,解决头文件的循环依赖性。源文件实际上没有循环依赖。 Stuffcollection.cpp 需要知道类 Spritesheet (因为你解引用它),所以你需要包含定义该文件中 Spritesheet 类的头。 p> 从您之前的Q 此处 ,我相信 Stuffcollection 是在 Spritesheet 头文件的类声明中使用,因此上述建议的解决方案。 In C++, I have a problem with circular dependencies / incomplete types. The situation is as follows:Stuffcollection.h#include "Spritesheet.h";class Stuffcollection { public: void myfunc (Spritesheet *spritesheet); void myfuncTwo ();};Stuffcollection.cppvoid Stuffcollection::myfunc(Spritesheet *spritesheet) { unsigned int myvar = 5 * spritesheet->spritevar;}void myfunc2() { //}Spritesheet.h#include "Stuffcollection.h"class Spritesheet { public: void init();};Spritesheet.cppvoid Spritesheet::init() { Stuffcollection stuffme; myvar = stuffme.myfuncTwo();}If I keep the includes as shown above, I get the compiler errorspritesheet has not been declared in Stuffcollection.h (line 4 inthe above). I understand this to be due to a circular dependency.Now if I change #include "Spritesheet.h" to the ForwardDeclaration class Spritesheet; in Stuffcollection.h, I get thecompiler error invalid use of incomplete type 'struct Spritesheet'in Stuffcollection.cpp (line 2 in the above).Similarly, if I change #include "Stuffcollection.h" to classStuffcollection; in Spritesheet.h, I get the compiler error aggregate'Stuffcollection stuffme' has incomplete type and cannot be definedin Spritesheet.cpp (line 2 in the above).What can I do to solve this problem? 解决方案 You should include Spritesheet.h in Stuffcollection.cppJust use forward declaration in the header file not the cpp file, that solves the circular dependency of the header file. The source file has no circular dependency actually.Stuffcollection.cpp needs to know the complete layout of class Spritesheet(because you dereference it), So you need to include the header which defines the class Spritesheet in that file.From your previous Q here, I believe that class Stuffcollection is used in the class declaration of Spritesheet header file and hence the above proposed solution. 这篇关于循环依赖性/不完全类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-09 11:02