我正在尝试制作一个简单的游戏引擎。我以前从未使用过 OOP,所以这可能是一个简单的错误,但我在尝试创建类的实例时遇到了这个错误。
invalid conversion from `World*' to `int'
initializing argument 1 of `World::World(int)'
这是创建类的代码。
World w = new World(100);
和实际的类:
class World {
int maxParts;
public:
GameObject **parts;
World(int maxParts);
int addObject(int type);
private:
int firstFreeId();
};
World::World(int maxParts)
{
parts = new GameObject *[maxParts];
}
...
感谢您的帮助。
最佳答案
new World(100)
动态创建一个新的 World
对象,将 100 传递给构造函数,但评估为指向创建的指针对象。
如果你只是想构造一个World
对象你不需要new
表达式,你可以直接构造它:
World w(100);
您收到的错误是因为您试图根据 new
表达式返回的 World
指针构造一个 World
对象。唯一的 World
构造函数接受一个 int
,如果没有显式转换,您不能将指针类型转换为 int
。
关于c++ - 类的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1370258/