C ++ - 在一行中定义多个对象指针导致编译器错误 [英] C++ - Declaring multiple object pointers on one line causes compiler error

查看:166
本文介绍了C ++ - 在一行中定义多个对象指针导致编译器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我这样做(在我的课)

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>)))

,然后它说对考生 re_sprite_eyes SF ::雪碧的对象和/或引用。

为什么这个不工作?不声明一样呢?

Why does this not work? Aren't the declarations the same?

推荐答案

SF ::雪碧* re_sprite_hair,re_sprite_body,re_sprite_eyes;

不声明三分球 - 这是一家指针和2对象

Does not declare 3 pointers - it is one pointer and 2 objects.

SF ::雪碧* 遗憾的是并不适用于声明的所有变量以下,只是第一个。它相当于

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;

您想要做的:

SF ::雪碧* re_sprite_hair,* re_sprite_body,* re_sprite_eyes;

您需要把一星为每个变量。在这种情况下,我preFER保持对变量的身边的明星,而不是类型,使正是这种情况清楚了。

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.

这篇关于C ++ - 在一行中定义多个对象指针导致编译器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆