编译器在给出生命周期时要求struct的生命周期 [英] Compiler asking for lifetime in struct when lifetime is given
问题描述
我正在尝试在"SFML游戏开发"一书中编写示例,但是对于应该代表游戏世界的结构的生存期存在问题.错误如下:
I'm trying to write the examples in the book "SFML Game Development" but I'm having a problem with the lifetimes for the struct that's supposed to represent the game world. The error is as follows:
extern crate sfml;
use self::sfml::window::*;
use self::sfml::graphics::*;
pub struct Game<'s> {
mWindow: RenderWindow,
mPlayer: &'s CircleShape,
}
错误消息:
error[E0106]: missing lifetime specifier
--> src/game.rs:8:18
|
8 | mPlayer: &'s CircleShape,
| ^^^^^^^^^^^ expected lifetime parameter
为什么要给我一辈子?
推荐答案
为什么要给我一辈子?
Why is it asking for a lifetime if I have given it one?
因为您没有在需要的时间里使用它.仔细查看错误消息.它告诉您CircleShape
缺少生命周期,而不是
Because you haven't given it the lifetime where it's needed. Look closely at the error message. It's telling you that CircleShape
is missing a lifetime, not the reference to CircleShape
(although that's also needed).
查看CircleShape
的定义:
pub struct CircleShape<'s> { /* fields omitted */ }
已对其进行了生命周期参数化,因此您需要提供一个参数:
It has been parameterized by a lifetime, so you need to provide one:
pub struct Game<'s> {
mWindow: RenderWindow,
mPlayer: &'s CircleShape<'s>,
}
对于您的情况,是否正确,我不能说,但是应该编译.
Whether that's correct for your case, I can't say, but it should compile.
这篇关于编译器在给出生命周期时要求struct的生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!