Search code examples
c++tetris

How do I use a variable from a function of a cpp file on another function in anotehr cpp file?


I'm making a tetris game and I can't figure out how to use the variable that calculates the score in another file where it is going to print the score to the screen. Here is the code:

//board.cpp
//Here is the variable mScore that i want to use

/* 
======================================                                  
Delete all the lines that should be removed
====================================== 
*/
int Board::DeletePossibleLines ()
{
     int Score = 0;
     for (int j = 0; j < BOARD_HEIGHT; j++)
     {
         int i = 0;
         while (i < BOARD_WIDTH)
         {
            if (mBoard[i][j] != POS_FILLED) break;
            i++;
         }

         if (i == BOARD_WIDTH)
         {
             DeleteLine(j);
             Score++;
         }
     }
     int mScore = Score;
     return mScore;
 }

It is declared in a class in Board.h as this:

//Board.h
class Board
    {
    public:
    int DeletePossibleLines();
    }

And i want to use it in IO.cpp, i treid this:

//IO.cpp
#include "Board.h"

void IO :: text()
    {
    //I call the class
    Board *mBoard
    //I attribute the function to a variable and i get an error
    int Score = *mBoard -> DeletePossibleLines;
    }

The error i get is "Error C2276: '*' : illegal operation on bound member function expression" on the IO.cpp

So i want Score from IO.cpp to be equal to mScore from Board.cpp

Here's also what I tried and failed if it helps:

I tried to declare the class in IO.cpp like this:

  //IO.cpp
  Board mBoard
  mBoard.DeletePossibleLines

But got an error that says "Expression must have a class type"

I also tried putting everything in the same file, but I failed as well, plus each file has more that a hundred lines of code.


Solution

  • You must call the function, not assign the function's pointer. Use something like this:

    int Score = mBoard -> DeletePossibleLines();
    //                                       ^^ note these!
    

    This assumes a valid mBoard pointer which is not present in the code you posted.