我试图研究这个错误,但是如果某些东西无法转换为自身,实在令人困惑。虽然我知道为什么。
此功能出现错误:
//GameObject.h
#include "BasicComponents.h"
namespace mw
{
class GameObject
{
public:
void handleEvents(mw::World world, sf::RenderWindow& screen, mw::EventList& events)
{
myInput->handleEvents(*this, screen, events);
//Passing world here causes the error
myPhysics->handleEvents(*this, world, screen, events);
myGraphics->handleEvents(*this, screen, events);
}
};
}//end of namespace
获取错误的函数是:
//BasicComponents.h
namespace mw
{
//Needed for parameters
class GameObject;
class World;
///////////////////////
class PhysicsComponent : public mw::Component
{
public:
virtual void handleEvents(GameObject& object, mw::World world, sf::RenderWindow& screen, mw::EventList& events) = 0;
};
}//end of namespace
我认为问题在于BasicComponents.h中使用的不完整类型。但是,我需要让函数知道该类存在于某个地方,并且当我们知道它时我们会用它做一些事情。如果这是问题,我该如何解决?否则,为什么会发生这种情况?
编辑:复制构造函数:
//World.h
//Copy Constructor
World(const World& me)
{
myMap = me.myMap;
myObjects = me.myObjects;
}
MCVE:
// Class1.h
#pragma once
#include "Class2.h"
namespace test
{
class Class1
{
public:
void function(test::Class3& c3)
{
myC2->function(*this, c3);
}
Class1() {}
~Class1(){}
private:
Class2* myC2;
};
}//end of namespace
// Class2.h
#pragma once
namespace test
{
class Class1;
class Class3;
class Class2
{
public:
virtual void function(Class1 c1, Class3 c3) = 0;
void otherFunction(){}
Class2(){}
~Class2(){}
private:
};
}//end of namespace
答案 0 :(得分:2)
您必须看到void handleEvents(mw::World world, sf::RenderWindow& screen, mw::EventList& events)
关于mw::World
不完整的某种警告/错误。
传递具有pass-by-value的类需要知道类的声明,以便知道参数需要多少空间。即使您可以欺骗编译器,该类的大小在那时也是未知的,因此将为参数保留错误的空间量。
传递一个带有pass-by-reference的类并没有这个限制,即使没有定义类,参数所需的空间也是已知的大小。
如果可能传递const mw::World& world
而不是mw::World world
,通过将其从值传递更改为传递引用,GameObject.h将能够使用{的前向声明{ {1}}。
最后,我还建议将内联mw::World
移动到BasicComponents.cpp,等到内联代码之前进行测试,并且只有导致性能问题的内联代码。正如Knuth所说,"过早优化是所有邪恶的根源。"