Array Issue In OOP

I am having a slight problem with OOP, and it does not allow me to use the unitialized variable of slidingBoard to put into the function tiles.InitializeBoard(slidingBoard);, but whenever I initialize slidingBoard it does not accept the array of 'int slidingBoard[rows][cols];' as an initialization. I was wondering if there is something I cannot see in this, or if there is an ability to simplify the code while still using OOP?

NOTE: I am separating the messages due to length of the Implementation

"Source.cpp" Driver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>				// for general IO
#include "Header.h"
using namespace std;
int main() {
	int** slidingBoard;		// the board that holds the sliding tiles
	int rows = 0, cols = 0;
	bool colvalid = false;
	bool rowvalid = false;
	Tile tiles;
	while (colvalid == false)
	{
		cout << "Enter Columns\n";
		cin >> cols;
		tiles.setColumns(cols);
	}
	while (rowvalid == false)
	{
		cout << "Enter Rows\n";
		cin >> rows;
		tiles.setRows(rows);
	}
	tiles.InitializeBoard(slidingBoard); // how would one go about allowing 'slidingBoard' to be accepted whilist utilizing the rows and columns?
	return 0;
}


"Header.h" Specification
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#ifndef __HEADER__
#define __HEADER__
using namespace std;
class Tile
{
private:
	int NUM_COLS;
	int NUM_ROWS;
public:
	Tile()
	{}
	bool setColumns(int);
	bool setRows(int);
	bool setPivot(int**);
	void InitializeBoard(int**);
};
#endif 

Last edited on

"Source1.cpp" Implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <cstdlib>
#include <iostream>
#include "Header.h"
#define PIVOT -1			// used to mark the pivot spot (blank area) on the puzzle
#define PIVOT_SYMBOL '*'
using namespace std;
bool Tile::setColumns(int myCols)
{
	bool accepted = false;
	if (myCols <= 0) {
		accepted = false;
		cout << "Invalid Column Count.\n";
	}
	else
	{
		accepted = true;
		NUM_COLS = myCols;
	}
	return accepted;
}
bool Tile::setRows(int myRows)
{

	bool accepted = false;
	if (myRows <= 0) {
		accepted = false;
		cout << "Invalid Column Count.\n";
	}
	else
	{
		accepted = true;
		NUM_ROWS = myRows;
	}
	return accepted;
}
bool Tile::setPivot(int** theBoard)
{
	int accepted = false;
	int pivotnum = NUM_COLS * NUM_ROWS;
	for (int x = 0; x < NUM_ROWS; x++)
	{
		for (int z = 0; z < NUM_COLS; z++)
		{
			if (theBoard[x][z] == pivotnum)
			{
				accepted = true;
				theBoard[x][z] = PIVOT;
			}
		}
	}
	return accepted;
}
void Tile::InitializeBoard(int** theBoard)
{
	// x is first counter, z is second counter.
	int enterno = 1; // enterno is declared as int initialized at 1.  // enterno means the number entered into the array
	for (int x = 0; x < NUM_ROWS; x++) // for x is declared as int initialized as 0, while x < NUM_ROWS, x goes up 1 when function loops
	{
		for (int z = 0; z < NUM_COLS; z++) // for z is declared as int initialized as 0, while z < NUM_COLS, z goes up 1 when function loops
		{
			theBoard[x][z] = enterno; // array[x][z] will equal enterno.
			enterno++; //enterno counts up 1
		}
	}
	setPivot(theBoard);
	// in the future, -1, or PIVOT, will equal PIVOT_SYMBOL
}
ADDITIONAL NOTICE: I am using Visual Studio 2015, sorry for anyone having issues attempting to solve this before-hand.
Last edited on
int slidingBoard[rows][cols];
standard c++ does not allow variable length arrays, only constants.

leave it as **, or consider using vectors.

that aside, where can't you use it if you don't have the above?

I THINK I see AN issue, but not sure if its the only one.
pointers work just like any other variable. a function that changes the value of a pointer that is passed in should have it as a reference.
void Tile::InitializeBoard(int** theBoard)
should be, I think,
void Tile::InitializeBoard(int** &theBoard) //Ill be honest, not 100% sure this is correct, I pretty much refuse to use double pointers because of the extra complexity they generate. I am pretty sure something there needs to be a reference, but not exactly where the & goes.

changing the data pointed to is free (don't need references) as you don't change the pointer's value itself, you change what it points to, which isnt governed by the reference/copy syntax.

Without the reference, you change a copy of the pointer, but not the original passed in value..



Last edited on
If you are talking about the initialization of int slidingBoard[rows][cols]; it right before tiles.InitializeBoard(slidingBoard); , eitherwise this code above gives me an error that states
unitialized local variable 'slidingBoard' used
and specifies the line at which tiles.InitializeBoard(slidingBoard); is called.
Also realized recently that colvalid=tiles.setColumns(cols); should be implemented, aswell as rowvalid=tiles.setRows(rows);, therefore it runs smoothly notheless
oh, I see.
int** slidingBoard; this is in main.
it is passed into initialize:
theBoard[x][z] = enterno; //theboard is really sliding board, but its uninitialized.

but you didnt allocate any memory for it anywhere. you can't do this. you need to use a new statement to allocate memory for your pointers, and because you did double pointer, you need to do that in a loop. and you need a looped delete as well to destroy it after.

to recap ... initialize should probably allocate the memory, and as a pointer reference as I said the first time.

on top of this, it really, really seems like the int** should be a class member, not a stray variable that is tightly coupled to your class. Something seems very wrong on the design side. It will work -- you can get all kinds of stuff to work -- but tightly coupled disparate things is usually a recipe for problems.
Last edited on
New issue now: I am able to enter a number of rows and columns, but I am provoked by a message
Exception thrown at 0x01182D7B in SlidePuzzle.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

If there is a handler for this exception, the program may be safely continued.

@jonnin, if that is the case should I rather state 'theBoard' only in the Implementation? and remove slidingBoard from the Driver?
Last edited on
Thanks for your help @jonnin , your help definitely solved my issue with this dilemma, you have my sincere gratitude.
Glad to help. This is a great example of why a vector would have been the better choice; the trouble you had is exactly why pointers are frowned upon, it is too easy to make mistakes with them. So you get two lessons in one, how to do the pointer stuff, and why not to :)
Topic archived. No new replies allowed.