如何在C 中调用此lambda功能

How to call this Lambda function in C++

本文关键字:lambda 功能 调用      更新时间:2023-10-16

i在受保护的void 函数

class Tetris: protected TetrisArea<true>
{
public:
    Tetris(unsigned rx) : TetrisArea(rx), seq(),hiscore(0),hudtimer(0) {}
    virtual ~Tetris() { }
protected:
    // These variables should be local to GameLoop(),
    // but because of coroutines, they must be stored
    // in a persistent wrapper instead. Such persistent
    // wrapper is provided by the game object itself.
    Piece seq[4];

lambda功能,

auto fx = [&seq]() {  seq[0].x=4;       seq[0].y=-1;
                        seq[1].x=Width;   seq[1].y=Height-4;
                        seq[2].x=Width+4; seq[2].y=Height-4; }; 

所以这是问题。我有这些错误:

 error: capture of non-variable 'Tetris::seq' 
         auto fx = [&seq]() {  seq[0].x=4;       seq[0].y=-1;
 error: 'this' was not captured for this lambda function
     auto fx = [&seq]() {  seq[0].x=4;       seq[0].y=-1;

..,以及随后在功能中的seq [n]引用。

我试图直接在受保护的void 函数中键入代码,但是尽管它编译了,但它似乎并不能正常工作,因为该程序来自他的Tetris DOS游戏中的YouTube Channel Bisqwit。

时,您会尝试捕获对象的成员而不捕获对象本身。将[&seq]更改为[this],看看会发生什么。