473,734 Members | 2,824 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer Matrix

hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

http://www.sfu.ca/~mnb2/matrix.gif

and here's the relevant code I'm working with (doesn't work)

class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filena me);
}
};

Tile square("img/square.png");
Tile triangle("img/triangle.png");

class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];

I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually... . like so

for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}

I can't seem to figure out the proper syntax...

also..how would I properly delete this?
delete [][]tile?

Mar 10 '07 #1
4 5538
On Mar 9, 6:19 pm, "Mark" <mnbaya...@gmai l.comwrote:
hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

http://www.sfu.ca/~mnb2/matrix.gif

and here's the relevant code I'm working with (doesn't work)

class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filena me);
}

};

Tile square("img/square.png");
Tile triangle("img/triangle.png");

class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];

I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually... . like so

for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}

I can't seem to figure out the proper syntax...

also..how would I properly delete this?
delete [][]tile?
the code below makes more sense, but still doesn't compile

Tile (*tile)[16][16];

public:
Map()
{
for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = &square;
break;
case 1:
tile[x][y] = &triangle;
break;
}
}
}
}

Mar 10 '07 #2
Mark wrote:
hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

http://www.sfu.ca/~mnb2/matrix.gif

and here's the relevant code I'm working with (doesn't work)

class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filena me);
}
};

Tile square("img/square.png");
Tile triangle("img/triangle.png");

class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];

I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually... . like so

for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}

I can't seem to figure out the proper syntax...

also..how would I properly delete this?
delete [][]tile?
Why are you allocating this matrix? It's 16x16 always so there is no
need to allocate.

class Map
{
Tile* tile[16][16];
};

If you really did want to allocat then it would be

class Map
{
Tile* (*tile)[16];
Map()
{
tile = new Tile*[16][16];
}
};

This only works because the first dimension of your array is constant.

You delete the same way you delete any array allocation

delete[] tile;

john
Mar 10 '07 #3
On Mar 9, 11:57 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
Mark wrote:
hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...
http://www.sfu.ca/~mnb2/matrix.gif
and here's the relevant code I'm working with (doesn't work)
class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filena me);
}
};
Tile square("img/square.png");
Tile triangle("img/triangle.png");
class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];
I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually... . like so
for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}
I can't seem to figure out the proper syntax...
also..how would I properly delete this?
delete [][]tile?

Why are you allocating this matrix? It's 16x16 always so there is no
need to allocate.

class Map
{
Tile* tile[16][16];

};

If you really did want to allocat then it would be

class Map
{
Tile* (*tile)[16];
Map()
{
tile = new Tile*[16][16];
}

};

This only works because the first dimension of your array is constant.

You delete the same way you delete any array allocation

delete[] tile;

john
erm..i discovered a solution, hence the deleted posts. i wasn't sure
if it was necessary to allocate memory for them or not, and ideally i
don't want a constant 16x16 grid, that was just for sample. decided
to use a <vector<vector< Tile*
but thank you for the help :)

Mar 13 '07 #4
Mark wrote:

erm..i discovered a solution, hence the deleted posts.
For future reference, that doesn't really do much except remove them
from the Google archives. Most of the participants here are not reading
via GG, and your deletion doesn't necessarily affect the copies on
other servers. I'm not entirely sure if that sends a cancel message or
not.

In general, it's best not to try to delete them, as you get an
indeterminant state where some people see the referenced message and
others don't.

Brian
Mar 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1355
by: Tony Johansson | last post by:
Hello experts! Assume we have the definition of class Matrix below. Now to my question how is it possible to have this expression (*this = m;) in the copy constructor part of class Matris. Isn't this a constant pointer to the current object and can't be changed. Im I wrong? class Matrix {
9
15717
by: sangeetha | last post by:
Hello, Is there any performance difference in using of the following two declaration? int (*ptr); //Array of 10 int pointers int *ptr; // pointer-to-array of 10. Regards, Sangeetha.
7
2045
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this method creates a new object (a Matrix) for it. I suppose that after the new operator, my pointer is pointing to an object, so when the method has finished, the very first pointer is still poitint to the created method; however this is not working,...
11
2294
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
11
1538
by: Army1987 | last post by:
Is this code legal (according to the strictest possible sane interpretation of the standard, regardless of wheter it does work on all implementations in the known universe)? #include <stdio.h> void print_square_matrix(int *ptr, int order) { int i, j; for (i = 0; i < order; i++) { for (j = 0; j < order; j++)
5
9638
by: Anolethron | last post by:
Wrong one: void minptr (int *matrix, int rows, int columns,int *min){ int i=0,j=0; *min=*matrix; //!!!!!!!!!!!!!!!!! for (i=0; i < rows; i++) { for (j=0; j < columns; j++) { if( *min *(matrix+(i*columns)+j) ) { min = (matrix+(i*columns)+j);
6
1515
by: Matt | last post by:
I'm almost embarrassed to ask this because it seems like a complete newbie question, but here goes. How do I find out the size of an array of pointers that defines a 2D matrix called 'matrix0' when the only declaration I can find is "double **matrix0". After that declaration the next time the matrix is used is to assign values from: "matrix0 = stamps.mat;"
17
5257
by: Ivan K. | last post by:
I am looking at some legacy code, which begins by allocating a double matrix with the dmatrix() function from NRC as follows: double **A, **augin, **augout, **aa; A = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3); aa = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3); augin = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS); augout = dmatrix(1, MAXNSTU+1, 1, MAXCOV+MAXCOVLOC+3+MAXSIMS);
4
1820
by: Encrypted | last post by:
i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. after the addition i want to return the local object "result" through a pointer (and not call by value)..however i aint getting d values of the result in main()...instead i am getting garbage values when i print it... here is my code : #include <iostream.h> #include <stdio.h> #include...
17
2736
by: Andrea Taverna (Tavs) | last post by:
Subject: Initialization of a const matrix implemented as pointer-to-pointer Hello everyone. I've got the following matrix definition in a source file static const char **a; I need it to be initialized as an array of strings, something like
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.