This question already has answers here:
Using member variable in lambda capture list inside a member function
                                
                                    (5个答案)
                                
                        
                                去年关闭。
            
                    
我在此类的受保护的void函数中编写了lambda函数

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频道Bisqwit,因此似乎无法正常工作。

最佳答案

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

关于c++ - 如何在C++中调用此Lambda函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48808297/

10-11 20:37