0

I was writing a lambda function inside a protected void function of this class

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];

The lambda function,

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; }; 

So here's the problem. I got these errors:

 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;

.. and also for subsequent reference of seq[n] in the function.

I tried to type the code directly in the protected void function but although it compiles, it doesn't seem to work normally as the program is from the Youtube channel Bisqwit in his Tetris Dos game.

scholar guy
  • 79
  • 2
  • 11

1 Answers1

2

As it reads, you try to capture an object's member without capturing the object itself. Change [&seq] into [this] and see what happens.

bipll
  • 11,747
  • 1
  • 18
  • 32
  • Thanks it worked anyway! Really appreciated! – scholar guy Feb 15 '18 at 13:26
  • If you can use C++14, there's a new way using initialized lambda capture expressions: `auto fx = [&ref = seq]() { ref[0].x=4; etc. }` – The Vee Feb 15 '18 at 14:01
  • 1
    Actually, it can say `[&seq = seq]` and then proceed as the original version. Such a simple name reuse is applied in [this answer](https://stackoverflow.com/a/42029220/1537925) to the linked question. – The Vee Feb 15 '18 at 14:09