I've seen this problem a few times and I've tried what they've done. I am still not passing my Array correctly.
I'm all templated up because of last files I have used for this program (Not in the code). If that could have something to do with it let me know.

This is the base code I was working with. My Edited code became to tangled I couldn't work with it. So if something is a tiny error it's because it was rebuild quickly and haphazardly so I apologize in advance if there are any of those problems.

template <class T>
class Boggle
{
public:
	Boggle(void);
	~Boggle(void);

	int ** MyBoard;
	int size;
	int Rows;
	int Cols;

	void SizeBoard();
	void PrintBoard();
};

template <class T>
Boggle<T>::Boggle(void)
{
	Size = 0;
	Rows = 0;
	Cols = 0;
}
template <class T>
Boggle<T>::~Boggle(void)
{
}

template <class T>
void Boggle<T>::SizeBoard()
{
	int size;
        Rows = 0;
	cout << "Enter your Boggle Board Size. (Please input one number)."<<endl;
	cin >> size;

	MyBoard = new int*[size];

	for (Rows=0; Rows<size; Rows++)
	{
		MyBoard[Rows] = new int[size];
	}

	for(Rows=0; Rows<size; Rows++)
	{
		for(int Cols=0; Cols<size; Cols++)
		{
			MyBoard[Rows][Cols] = Cols;
		}
	}
}

template <class T>
void Boggle<T>::PrintBoard()
{
	
	for (int Rows = 0; Rows < size; Rows++)
	{
		for (int Cols = 0; Cols < size; Cols++)
			cout << MyBoard[Rows][Cols] << " ";

		cout << endl;
	}
}

When I Print my Array nothing ever comes out. I used a couple different functions with dummy cout codes to see if it's passing and I've tried alot of different things. I've definitely spent alot of time working on this.
The Main simply has the functions running in it for testing.
When I put these both in the main they work just fine. Which leaves me to believe it's a passing error in my class.
Normally this would be an easy problem however I don't normally Template or work with 2D arrays so I'm kind of out of my element.

Recommended Answers

All 4 Replies

Hi, im new myself to the language, and haven't covered classes yet but by looking at the function PrintBoard shouldn't you want to pass it MyBoard array ? I would assume once function SizeBoard finishes execution MyBoard array gets destroyed and when you call PrintBoard it doesnt have anything.

Hi, im new myself to the language, and haven't covered classes yet but by looking at the function PrintBoard shouldn't you want to pass it MyBoard array ? I would assume once function SizeBoard finishes execution MyBoard array gets destroyed and when you call PrintBoard it doesnt have anything.

I've been trying that a bunch of ways. Nothing seemed to change, however I will try something right now and see what happens.

Well I could be doing this wrong. Tried this, still no result.

int** SizeBoard();
	void PrintBoard(int** MyBoard);

template <class T>
int** Boggle<T>::SizeBoard()
{
	int Size;
	cout << "Enter your Boggle Board Size. (Please input one number)."<<endl;
	cin >> Size;

	MyBoard = new int*[Size];

	for (Rows=0; Rows<Size; Rows++)
	{
		MyBoard[Rows] = new int[Size];
	}

	for(Rows=0; Rows<Size; Rows++)
	{
		for(int Cols=0; Cols<Size; Cols++)
		{
			MyBoard[Rows][Cols] = Cols;
		}
	}

	return MyBoard;
}

template <class T>
void Boggle<T>::PrintBoard(int **MyBoard)
{
	
	for (int Rows = 0; Rows < Size; Rows++)
	{
		for (int Cols = 0; Cols < Size; Cols++)
			cout << MyBoard[Rows][Cols] << " ";

		cout << endl;
	}
}

int main()
{
	Boggle<int>boggle;

	boggle.SizeBoard();
	boggle.PrintBoard(boggle.MyBoard);
}

(Some bits have been edited out just for easy read)

Your problem is that you have redeclared size inside the member function Boggle<T>::SizeBoard() . If you delete the int size on line 7 you'll be OK. You have to remember that, in C++, the most local variable is the one that is used, unless you specifically do something about it.

I know that you said that this isn't your final code, but I have to comment and say that your member variable should be private, not public. Also, why does SizeBoard() return a pointer to MyBoard ? Finally, you should take the user interface stuff out of SizeBoard() and put it in the calling function. So you can do something like:

int boardSize;
std::cout << "Enter the size of the board: ";
std::cin >> boardSize;
B.SizeBoard(boardSize);

Hope that helps a little :)

Ah Haha... Of course it was that easy. I knew it was something silly like that. Thanks so much.
Yah this was a heap of code I found, i wanted it to work before i started manipulating it. Thanks so much that's lots of hours saved XP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.