简体   繁体   English

给出生命周期的编译器在结构中询问生命周期

[英]Compiler asking for lifetime in struct when lifetime is given

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. 我正在尝试在“ SFML游戏开发”一书中编写示例,但是对于应该代表游戏世界的结构的生命周期存在问题。 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 message: 错误信息:

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? 如果我给了我一生,为什么要一生?

Why is it asking for a lifetime if I have given it one? 如果我给了我一生,为什么要一生?

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缺少生命周期,而不是对CircleShape引用 (尽管这也是必需的)。

Review the definition of CircleShape : 查看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. 我不能说这是否适合您的情况,但是应该编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM