Creating A Multidimentional Array Of Classes
Moderator: Coders of Rage
- davidthefat
- Chaos Rift Maniac
- Posts: 529
- Joined: Mon Nov 10, 2008 3:51 pm
- Current Project: Fully Autonomous Robot
- Favorite Gaming Platforms: PS3
- Programming Language of Choice: C++
- Location: California
- Contact:
Creating A Multidimentional Array Of Classes
I feel like a total noob asking this, but I was unsuccessful with making a 2d array of classes. I would get runtime errors, but not compiler errors. I tracked down the error using a debugger to a initialization of a 2d array of classes using pointers. I will post the code as soon as I can get my hands on it (its on a school computer)
Re: Creating A Multidimentional Array Of Classes
Without code there is not much to say. You could use std::vector though:
Code: Select all
std::vector< std::vector< xyz > > myDynamic2dArray;
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Creating A Multidimentional Array Of Classes
Unless you NEED it to be dynamic, you don't want to use vectors. And even if you need it to be dynamic, you only want to use vectors if they have a significant advantage over simple dynamic arrays.K-Bal wrote:Without code there is not much to say. You could use std::vector though:
Code: Select all
std::vector< std::vector< xyz > > myDynamic2dArray;
As for the OP's problem, without more informationt there isn't anything to do. To quote Sherlock Holmes, "One should never theorize until he has data."
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: Creating A Multidimentional Array Of Classes
There are many ways to do it. I like it for it's simplicity and would never use it in high performance programs.Ginto8 wrote: Unless you NEED it to be dynamic, you don't want to use vectors. And even if you need it to be dynamic, you only want to use vectors if they have a significant advantage over simple dynamic arrays.
- davidthefat
- Chaos Rift Maniac
- Posts: 529
- Joined: Mon Nov 10, 2008 3:51 pm
- Current Project: Fully Autonomous Robot
- Favorite Gaming Platforms: PS3
- Programming Language of Choice: C++
- Location: California
- Contact:
Re: Creating A Multidimentional Array Of Classes
Code: Select all
//Image.cpp
#include "Image.h"
Image::Image(int H, int W)
{
Height = H;
Width = W;
**Points = *new Location[Height]; //This is the problem
for(int c = 0; c < H; c ++)
{
*Points[c] = *new Location[Width];
}
for(int h = 0; h < H; h ++)
{
for(int w = 0; w < W; w ++)
{
Points[h][w].setLocation(*new Location((float)w / 3 , (float)h / 3, 0));
}
}
}
Image::~Image()
{
//dtor
}
float Image::getX(int H, int W)
{
return Points[H][W].getX();
}
float Image::getY(int H, int W)
{
return Points[H][W].getY();
}
float Image::getZ(int H, int W)
{
return Points[H][W].getZ();
}
int Image::getHeight()
{
return Height;
}
int Image::getWidth()
{
return Width;
}
Code: Select all
//Image.h
#include "Location.h"
class Image
{
public:
Image(int H, int W);
virtual ~Image();
float getX(int H, int W);
float getY(int H, int W);
float getZ(int H, int W);
int getHeight();
int getWidth();
protected:
private:
Location **Points;
int Height;
int Width;
};
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Creating A Multidimentional Array Of Classes
You reeeeeaally might want to have a look here:
http://www.codeproject.com/KB/cpp/arrayDinamic.aspx
Sorry to be a cheap ass, but I've answered this question about 3 times this month in C and C++.
ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
http://www.codeproject.com/KB/cpp/arrayDinamic.aspx
Sorry to be a cheap ass, but I've answered this question about 3 times this month in C and C++.
THAT is a very bad idea. I'm pretty sure this is memory leak city, unless you are explicitly freeing the new locations when they're done being used. I'm fairly certain that you just want to instantiate the temporary variable on the stack.davidthefat wrote:Code: Select all
Points[h][w].setLocation(*new Location((float)w / 3 , (float)h / 3, 0));
ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
- Bakkon
- Chaos Rift Junior
- Posts: 384
- Joined: Wed May 20, 2009 2:38 pm
- Programming Language of Choice: C++
- Location: Indiana
Re: Creating A Multidimentional Array Of Classes
No shit, it's a problem. The syntax is entirely wrong.davidthefat wrote:Code: Select all
**Points = *new Location[Height]; //This is the problem
Code: Select all
Location*** Points = new Location**[COLS];
for(int i = 0; i < COLS; i++)
Points[i] = new Location*[ROWS];
for(int y = 0; y < COLS; y++)
for(int x = 0; x < ROWS; x++)
Points[x][y] = new Location(params);
for(int y = 0; y < COLS; y++)
for(int x = 0; x < ROWS; x++)
delete Points[x][y];
for(int i = 0; i < COLS; i++)
delete [] Points[i];
delete [] Points;
Re: Creating A Multidimentional Array Of Classes
You wanted to say heapGyroVorbis wrote: ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Creating A Multidimentional Array Of Classes
Ah, shit. You're right.K-Bal wrote:You wanted to say heapGyroVorbis wrote: ALWAYS beware of instantiating temporary variables on the stack--unless whatever is receiving them is also deleting them, you are losing your reference to the pointer and can never free it.
- davidthefat
- Chaos Rift Maniac
- Posts: 529
- Joined: Mon Nov 10, 2008 3:51 pm
- Current Project: Fully Autonomous Robot
- Favorite Gaming Platforms: PS3
- Programming Language of Choice: C++
- Location: California
- Contact:
Re: Creating A Multidimentional Array Of Classes
Unfortunately, that has the same error... IDK what to do... Its due on Wednesday and I still have to get the socket connection working too...Bakkon wrote:No shit, it's a problem. The syntax is entirely wrong.davidthefat wrote:Code: Select all
**Points = *new Location[Height]; //This is the problem
Code: Select all
Location*** Points = new Location**[COLS]; for(int i = 0; i < COLS; i++) Points[i] = new Location*[ROWS]; for(int y = 0; y < COLS; y++) for(int x = 0; x < ROWS; x++) Points[x][y] = new Location(params); for(int y = 0; y < COLS; y++) for(int x = 0; x < ROWS; x++) delete Points[x][y]; for(int i = 0; i < COLS; i++) delete [] Points[i]; delete [] Points;