9 Replies - 2313 Views - Last Post: 01 November 2010 - 10:59 PM Rate Topic: -----

#1 UnshiftedShark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-November 10

Dynamic 2d arrays

Posted 01 November 2010 - 09:38 PM

I am having understanding dynamic 2d arrays. I just learned about them but am still confused because i can't seem to get it to work in my maze class. The 2d array is suppose to be able to hold a maze which the size can change. When i run it though i get
Error 1 error C2440: '=' : cannot convert from 'char' to 'char **'

Heres my code
maze.h file I only included the constructor and private members
class maze
{
  public:
     maze();
  private:
     char **mazeArr; // holds the configuration of the maze
     int numRows; // number of rows (y values) in the maze
     int numCols; // number of columns (x values) in the maze
     point mazeEnter; // location of the maze entrance
     point mazeExit; // location of the maze 
     mouse creature; // mouse in the maze
}



maze.cpp file maze constructor
#include<iostream>
#include"maze.h"
using namespace std; 

maze::maze() // default constructor
{
	numRows = 0;
	numCols = 0;
	for(int i =0; i<=numRows; i++)
	{
		for(int j = 0; j<=numCols; j++)
		{
			mazeArr = mazeArr[i][j];
		}
	}
}



Sorry if its sloppy post this is my first post. If you need anything else please let me know thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic 2d arrays

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dynamic 2d arrays

Posted 01 November 2010 - 09:48 PM

So what is this line trying to do?
mazeArr = mazeArr[i][j];



Explain it in words.
Was This Post Helpful? 0
  • +
  • -

#3 UnshiftedShark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-November 10

Re: Dynamic 2d arrays

Posted 01 November 2010 - 09:50 PM

i was simply trying to initiate the array so it would be able to store the number of rows and columns.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dynamic 2d arrays

Posted 01 November 2010 - 09:58 PM

Well what it is doing is this.
"Get the character at location [ i ][ j ]'
"Make the 2D array equal to that character"
As I am sure you can see, that will never work.

I am sorry but I don't know what these words
"initiate the array so it would be able to store the number of rows and columns"
mean exactly.

Are you saying you are trying to set the dimensions of the array?
I'm sorry, I really don't understand.

This post has been edited by janotte: 01 November 2010 - 09:59 PM

Was This Post Helpful? 0
  • +
  • -

#5 UnshiftedShark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-November 10

Re: Dynamic 2d arrays

Posted 01 November 2010 - 10:04 PM

Yes thats what I'm trying to do. Sorry for the misunderstanding. I am trying to set the dimensions of the array. I was thinking that I could just set the **mazeArr dimensions by seting it as mazeArr[i][j] where [i] would equal the number of rows and [j] would equal the number of columns.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dynamic 2d arrays

Posted 01 November 2010 - 10:14 PM

So now you know that won't work what is your next thought about how to create a dynamic array where the dimensions are 'numberOfRows' by 'numberOfColumns'?
Was This Post Helpful? 0
  • +
  • -

#7 UnshiftedShark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-November 10

Re: Dynamic 2d arrays

Posted 01 November 2010 - 10:24 PM

Okay so my method i had won't work. So could i declare the dimensions like
 mazeArr = new mazeArr[numColumn][numRow]



or is this saying create a new mazeArr?
Was This Post Helpful? 0
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dynamic 2d arrays

Posted 01 November 2010 - 10:34 PM

It is creating a new dynamic array. You are correct.
Isn't that what you want to do?
I think it is but maybe you could clarify that it isn't and why.

I am very worried that you seem to be trying to create a default constructor with dimensions of zero by zero.
That's a very bad idea.

Do you absolutely have to use dynamic arrays?
In C++ the more obvious choice would be vector.
http://www.cplusplus...nce/stl/vector/
Was This Post Helpful? 1
  • +
  • -

#9 UnshiftedShark  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-November 10

Re: Dynamic 2d arrays

Posted 01 November 2010 - 10:44 PM

Yeah i have to use a dynamic array because i have to read in the maze dimensions from a file but reading it in is a different constructor and i can't really start on that until i have this right. I just set it equal to zero to see if it would pass an integer. I can see thats a really bad idea now thinking about. The link for some reason gives me a blank page though.

sorry nevermind i got it to work. Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#10 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Dynamic 2d arrays

Posted 01 November 2010 - 10:59 PM

Excellent news.
Well done.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1