473,698 Members | 2,409 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 5536
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
1354
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
15716
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
2043
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
2290
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
1532
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
9635
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
1513
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
5249
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
1818
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
2730
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
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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
7737
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
6525
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
5861
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
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.