C++ 未初始化的引用成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10956139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uninitialized reference member
提问by K-ballo
Basically what I want to do is store the reference to the active animation of a sprite as a private member in the Actor class. I want to use a reference so I don't have to actually create the animation multiple times but I keep getting an error.
基本上我想要做的是将精灵的活动动画的引用存储为 Actor 类中的私有成员。我想使用参考,这样我就不必多次实际创建动画,但我一直收到错误消息。
Actor class declaration:
演员类声明:
class Actor
{
public:
Actor();
~Actor();
void setActiveAnimation(Animation anim);
void draw(sf::RenderWindow& win);
private:
sf::Sprite sprite;
MaJR::Animation& activeAnimation;
};
Actor class implementation:
Actor类实现:
Actor::Actor()
{
// constructor
}
Actor::~Actor()
{
// destructor
}
void Actor::setActiveAnimation(Animation anim)
{
activeAnimation = anim;
activeAnimation.gotoStart();
}
void Actor::draw(sf::RenderWindow& win)
{
sprite.setTexture(activeAnimation.getActiveFrame());
win.draw(sprite);
activeAnimation.nextFrame();
}
Build output:
构建输出:
/home/mike/MaJR Game Engine/src/Actor.cpp||In constructor 'MaJR::Actor::Actor()':|
/home/mike/MaJR Game Engine/src/Actor.cpp|8|error: uninitialized reference member 'MaJR::Actor::activeAnimation' [-fpermissive]|
||=== Build finished: 1 errors, 0 warnings ===|
回答by K-ballo
A reference cannot be reassigned, so it must be initialized at the member-initialization-list. However, you intend to reassign it so what you want is not a reference. What's more, in your setActiveAnimation
function you are setting such reference to a copy of the value passed as an argument, which leaves you with an invalid reference when the code exits the function. Perhaps a pointer would suit you?
不能重新分配引用,因此必须在成员初始化列表中对其进行初始化。但是,您打算重新分配它,因此您想要的不是参考。更重要的是,在您的setActiveAnimation
函数中,您将这样的引用设置为作为参数传递的值的副本,这会在代码退出函数时为您留下无效引用。也许指针会适合你?
In the class body:
在类体中:
MaJR::Animation* activeAnimation;
And the setActiveAnimation
function:
和setActiveAnimation
功能:
void Actor::setActiveAnimation(Animation* anim)
{
activeAnimation = anim;
activeAnimation->gotoStart();
}
回答by Patrick
When you have a member in your class that is a reference, you MUST initialize it in the constructor. Once it's initialized, you can't change it anymore (well you can change the value but not where it refers to).
当你的类中有一个引用的成员时,你必须在构造函数中初始化它。一旦它被初始化,你就不能再改变它(你可以改变值,但不能改变它所指的地方)。
Make activeAnimation a pointer instead.
将 activeAnimation 改为指针。
回答by betabandido
You cannot declare an uninitialized reference. That means you need to either initialized the reference when you create an object from class Actor
or you cannot use a reference. If you need to dynamically change it, then you could use a pointer or, probably better, a smart pointer(e.g., std::unique_ptr, shared_ptr). If you cannot use C++11, then have a look at smart pointersfrom Boost.
您不能声明未初始化的引用。这意味着您需要在从类创建对象时初始化引用,Actor
或者您不能使用引用。如果您需要动态更改它,那么您可以使用指针,或者更好的智能指针(例如std::unique_ptr、shared_ptr)。如果您不能使用 C++11,请查看Boost中的智能指针。
回答by Attila
You need to bind a reference at definition time. For member references this means in the initializer list of the constructor. Moreover the binding cannot be re-assigned after declaration.
您需要在定义时绑定一个引用。对于成员引用,这意味着在构造函数的初始化列表中。此外,声明后不能重新分配绑定。
If you don't have the object (to get the reference to) at instatiation time of Actor
objects, your next best bet is to use pointers which can be reassigned later on as needed.
如果您在对象的实例化时没有对象(以获取引用)Actor
,那么您的下一个最佳选择是使用稍后可以根据需要重新分配的指针。