0

I'm tackling this Tetris tutorial and so far understanding it piece by piece, but I have arrived at a point that has stumped me. This is the tutorial in question:

http://javilop.com/gamedev/tetris-tutorial-in-c-platform-independent-focused-in-game-logic-for-beginners/

My question regards a small issue of scope and functions. This is the Pieces class with the function in question, in a header file:

#ifndef _PIECES_
#define _PIECES_


class Pieces
{
public:

int GetBlockType        (int pPiece, int pRotation, int pX, int pY);
int GetXInitialPosition (int pPiece, int pRotation);
int GetYInitialPosition (int pPiece, int pRotation);
};

#endif // _PIECES_

Now here is the function GetBlockType() in a .cpp file.

int Pieces::GetBlockType (int pPiece, int pRotation, int pX, int pY)
{
    return mPieces [pPiece][pRotation][pX][pY];
}

The compiler tells me that mPieces is an undeclared identifier. There is a variable called mPieces, but I have declared it in main.cpp.

Is the problem an issue of scope? Have I declared mPieces in the wrong place? I've tried my hand at it and give up, so maybe someone here can help.

longAD
  • 53
  • 1
  • 6
  • Your include guard name is a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). Anyway, functions cannot access variables that are local to other functions. That would cause so many unavoidable conflicts. – chris Aug 07 '14 at 04:21
  • each cpp file is a separate compilation unit. If you want both `main.cpp` and `thisother.cpp` see `mPieces`, then you should declare it in a header file that both of those cpp files include. Why don't you make `mPieces` a member of the class? – triple_r Aug 07 '14 at 04:27
  • This is interesting stuff. The comments section of this tutorial I'm following all claim that he did a great job explaining everything, yet I seem to be only one with this problem, so he must have done something right. I've tried what you guys have suggested and I just can't figure out what the problem is on my end. – longAD Aug 07 '14 at 04:33
  • Nobody can figure this out? There was an answer earlier and we were making headway but I guess the author must have deleted it since it's gone now. – longAD Aug 07 '14 at 05:01
  • The `pieces.cpp` file from the tutorial declares the `char mPieces` multi-dimenional array at the top of the file, above the `int Pieces::GetBlockType()` method. Does your `pieces.cpp` have that? – j.w.r Aug 07 '14 at 05:11

1 Answers1

0

I downloaded the source code and can see where there might be some confusion. In Main.cpp, the program declares an mPieces object, but it is an instance of the class Pieces. A pointer to that object is then used to initialize Board and Game instances.

Main.cpp:

// Pieces
Pieces mPieces;

// Board
Board mBoard (&mPieces, mScreenHeight);

// Game
Game mGame (&mBoard, &mPieces, &mIO, mScreenHeight);

So then we look into the header and implementation files of Pieces. In the header file, we do not see any reference to any sort of data structure - so it is still not clear where the GetBlockType function is finding mPieces.

When we look into Pieces.cc, we see the mPieces that GetBlockType references is actually a hard-coded 4-dimensional char array:

Pieces.cpp:

#include "Pieces.h"

// Pieces definition
char mPieces [7 /*kind */ ][4 /* rotation */ ][5 /* horizontal blocks */ ][5 /* vertical blocks */ ] =
{
    // all the possible positions
}

// irrelevant code omitted

int Pieces::GetBlockType (int pPiece, int pRotation, int pX, int pY)
{
    return mPieces [pPiece][pRotation][pX][pY];
}

The char mPieces array is actually declared in global scope, so it should be accessible by any part of the program subsequent to #include "Pieces.h". In Main.cpp, the mPieces name is overridden in local scope by the Pieces mPieces declaration.

So now we see that in Main.cpp, mPieces is an instance of Pieces. The Pieces class itself, however, references a different mPieces - a char array that is defined in the Pieces.cpp class. You should verify that your Pieces.cpp has the correct mPieces array. I hope this helps!

Toby Liu
  • 1,267
  • 9
  • 14