在一行上声明多个对象指针会导致编译器错误 [英] Declaring multiple object pointers on one line causes compiler error
问题描述
当我这样做(在我的班级)
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 ;
我遇到这个编译错误:
错误:没有匹配'operator ='in'((Entity *)this) - > Entity :: re_sprite_eyes =(operator new(272u),(< statement>, sf :: Sprite *)< anonymous>)))
c $ c> re_sprite_eyes 是 sf :: Sprite
对象和/或引用。
为什么这不工作?
sf :: Sprite * re_sprite_hair,re_sprite_body,re_sprite_eyes;解决方案
不声明3个指针 - 它是一个指针和2个对象。
sf :: Sprite *
不幸地不适用于所有后面声明的变量,只是第一个。它等效于
sf :: Sprite * re_sprite_hair;
sf :: Sprite re_sprite_body;
sf :: Sprite re_sprite_eyes;
您要执行以下操作:
code> sf :: Sprite * re_sprite_hair,* re_sprite_body,* re_sprite_eyes;
您需要为每个变量放一个星。在这种情况下,我更喜欢将星保持在变量的侧面,而不是类型,以使这种情况清楚。
when I do this (in my class)
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;
Everything works fine. However, if I change the declarations to this:
private:
sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes;
I get this compiler error:
error: no match for 'operator=' in '((Entity*)this)->Entity::re_sprite_eyes = (operator new(272u), (<statement>, ((sf::Sprite*)<anonymous>)))
And then it says candidates for re_sprite_eyes
are sf::Sprite
objects and/or references.
Why does this not work? Aren't the declarations the same?
sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes;
Does not declare 3 pointers - it is one pointer and 2 objects.
sf::Sprite*
unfortunately does not apply to all the variables declared following it, just the first. It is equivalent to
sf::Sprite* re_sprite_hair;
sf::Sprite re_sprite_body;
sf::Sprite re_sprite_eyes;
You want to do:
sf::Sprite *re_sprite_hair, *re_sprite_body, *re_sprite_eyes;
You need to put one star for each variable. In such cases I prefer to keep the star on the variable's side, rather than the type, to make exactly this situation clear.
这篇关于在一行上声明多个对象指针会导致编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!