当我这样做(在我班上)
public: Entity() { re_sprite_eyes = new sf::Sprite(); re_sprite_hair = new sf::Sprite(); re_sprite_body = new sf::Sprite(); } private: sf::Sprite* re_sprite_hair; sf::Sprite* re_sprite_body; sf::Sprite* re_sprite_eyes;
一切都很好.但是,如果我将声明更改为:
private: sf::Sprite* re_sprite_hair,re_sprite_body,re_sprite_eyes;
我得到这个编译错误:
error: no match for 'operator=' in '((Entity*)this)->Entity::re_sprite_eyes = (operator new(272u),(<statement>,((sf::Sprite*)<anonymous>)))
然后它说re_sprite_eyes的候选人是sf :: Sprite对象和/或引用.
为什么这不行?声明不一样吗?
解决方法
sf :: Sprite * re_sprite_hair,re_sprite_eyes;
不声明3个指针 – 它是一个指针和2个对象.
sf :: Sprite *不幸的是不适用于在它之后声明的所有变量,只是第一个.相当于
sf::Sprite* re_sprite_hair; sf::Sprite re_sprite_body; sf::Sprite re_sprite_eyes;
你想做:
sf :: Sprite * re_sprite_hair,* re_sprite_body,* re_sprite_eyes;
你需要为每个变量放一个星.在这种情况下,我更愿意把变星的一面放在变数的一边,而不是把这个明确的事情弄清楚.