我一直在寻找答案 2 天;我完全不知所措。
我有...
struct shipDef
{
int mBaseHealth;
b2BodyDef mBodyDef;
};
...在另一个文件中...
b2Body* b2World::CreateBody(const b2BodyDef* def)
{
//...
}
……还有……
shipDef * Game::getShipDef(const char* shipDefID)
{
std::pair<std::multimap<std::string,shipDef*>::iterator, std::multimap<std::string,shipDef*>::iterator> iteratorPair = mShipDefMap.equal_range(shipDefID);
std::size_t shipDefIDSize = std::distance( iteratorPair.first, iteratorPair.second );
std::size_t randomSoundIndex = std::rand() % shipDefIDSize;
std::advance( iteratorPair.first, randomSoundIndex );
return iteratorPair.first->second;
}
我调用...
mPhysicsBody = physicsWorld->CreateBody(Game::getInstance()->getShipDef(shipType).mBodyDef);
我收到一个错误:
expression must have class type
调用 CreateBody 时。
我已经尝试了我能想到的“address of”运算符的每一种排列方式,而且我一直在谷歌和堆栈溢出中上下求索。
编辑以包含“b2BodyDef mBodyDef;”
最佳答案
如果 getShipDef
返回一个指针,您必须使用 ->
运算符来访问其成员:
mPhysicsBody = physicsWorld->CreateBody(
Game::getInstance()->getShipDef(shipType)->mBodyDef
);
如果 a
是一个指针,表达式 a->b
等同于 (*a).b
所以在你的情况下你可以也写过
mPhysicsBody = physicsWorld->CreateBody(
(*(Game::getInstance()->getShipDef(shipType))).mBodyDef
);
关于c++ - 表达式必须具有类类型 : c++ passing instance variable object as a pointer (dot notation parameter),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420021/