0

Hello and thanks in advance. I need to create a Tetris project in C++, i have the boards and pieces classes but i want to do an initializer class, so what im doing is this

#include "Initializer.h"

void Initializer::drawBoard()
{

}

// --------------------------------- Public Part ----------------------- //

Initializer::Initializer(Board *bBoard, Pieces *pPieces)
{

}

void Initializer::drawScene()
{
    bBoard->showBoard();
}

Here is the board class:

Board::Board(Pieces *mPieces)
{
    for(int x = 0; x < 20; x++)
    {
      for(int y = 0; y < 10; y++)
      {
        mBoard[x][y] = 0;
      }
    }
}

void Board::showBoard()
{
    for(int x = 0; x < 20; x++)
    {
        for(int y = 0; y < 10; y++)
        {
          if(mBoard[x][y] == 0)
          {
            cout << 0;
          }
          else
          {
            cout << "x";
          }
        }
        cout << endl;
    }

}

and the main function

#include "Pieces.h"
#include "Board.h"
#include "Initializer.h"
#include <iostream>
using namespace std;



int main(int numeroArgumentos, char *argumentos[])
{
  srand (time(NULL));
  int randomNumber = rand() % 6;
  Pieces mPieces;
  Board bBoard(&mPieces);
  Initializer iIOS(&bBoard, &mPieces);
  bBoard.showBoard();
  iIOS.drawScene();
  return 0;
}

the problem is, i get two different results when i execute the main function as you can see in the image (bBoard.showBoard is the left result, and the other one is the iIOS.drawScene()) enter image description here

What i have to do to get the correct board with the two functions?

4
  • Removed the C tag for you, since this is clearly not C. Commented Jun 15, 2018 at 22:46
  • 2
    Please post a minimal reproducible example. Commented Jun 15, 2018 at 22:47
  • 3
    Your Initializer constructor doesn't do anything with its arguments. Commented Jun 15, 2018 at 22:48
  • 1
    Use the debugger to take a look at bBoard in drawScene. Commented Jun 15, 2018 at 22:49

2 Answers 2

1

What you're getting is undefined behavior. Your constructor for Initializer isn't actually capturing the values in board, so it's presumably just running through memory that doesn't belong to it.

Sign up to request clarification or add additional context in comments.

Comments

1

The Initializer constructor needs to save its parameters in the appropriate member variables. Just naming the parameters the same as the member variables doesn't do this automatically. You can use a member initializer list to do this.

Initializer::Initializer(Board *bBoard, Pieces *pPieces): bBoard(bBoard), pPieces(pPieces) {}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.