Creating a 'new' multi-dimensional array

Started by
15 comments, last by cdmac 19 years, 1 month ago

Map = new CBase::sMap[Header.y][Header.x];
I tried using that statement in MSVC, and it's giving me:

CLoad.cpp(26) : error C2540: non-constant expression as array bound
CLoad.cpp(26) : error C2440: '=' : cannot convert from 'CBase::sMap (*)[1]' to 'CBase::sMap *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Map is a pointer of the sMap datatype there, but it doesn't seem to think it's right? I'm not sure what this means. I want to make a new array because I have to read the size of the data from file first, so if there is anyway I would appreciate it. Thanks!
-Conrad
Advertisement
Only the outermost size of an array can be dynamic.
Although there are various ways to emulate the syntax of multidimensional arrays (through pointer lists or overloaded operators) the easiest is to simply allocate Header.x * Header.y elements.
Map = new CBase::sMap[Header.x * Header.y];CBase::sMap getElement(Map *map, int x, int y) { return map[y * Header.x + x];}

This is the compiler stores them internally anyway, and as you might noticed in the code there's no way to tell the innermost dimension. C has the exact same problem, this is why your original code wouldn't work in the first place.
Quote:
Map = new CBase::sMap[Header.y][Header.x];
...
CLoad.cpp(26) : error C2540: non-constant expression as array bound


Header.x and Header.y are non-constant so your compiler is telling you that you can not use non-constants to define a multidimentionsl array.

I dont know what to say about the other error.
"Are you threatening me, Master Jedi?" - Chancellor Palpatine
How are you defining Map? I'll bet that it's not the proper type. Remember, arrays are pointers, thus Map should be defined (iirc)
CBase::sMap** Map;
(EDIT: CBase::sMap* Map; if you're going to use a single-dimenstional)

Your other problem would be the non-const values when sizing the array; I have no idea how to solve that, because I generally just use an STL container whenever I need to store data. (EDIT: Use a single-dimensional array. Can't believe 2 people beat me!)
In C++, array is spelled std::vector [wink]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by doynax
Only the outermost size of an array can be dynamic.

Untrue. Though it requires a little more work:
char **map = new char *[Header.y]; // char ** ~ 2 dimensional arrayfor ( unsigned i = 0; i < Header.y; ++i )  map = new char[Header.x];

Then you can access it like any 'normal' multidimensional array.

[edit 2]
Quote:Original post by doynax
through pointer lists

Sorry, didn't read that. I suppose you meant this?

[edit]
Quote:In C++, array is spelled std::vector

lol, the wierd syntax of C++.

Regards,
jflanglois
someone needs to make a "multi-dimentional array" sticky.
Yes, and it needs to reference this. (Heh... a section got added to that page of the FAQ, which broke my anchored bookmark... so if anyone is reading through my old posts and following a link to see "Is it legal (and moral) for a member function to say delete this?", just scroll down. [smile])
I was originally using a vector to store a struct array, but I'll be damned if I could figure out how the heck to get the whole array in there and not just the first element or w/e.
-Conrad
Quote:Original post by jflanglois
Quote:Original post by doynax
Only the outermost size of an array can be dynamic.

Untrue. Though it requires a little more work:
*** Source Snippet Removed ***
Then you can access it like any 'normal' multidimensional array.

[edit 2]
Quote:Original post by doynax
through pointer lists

Sorry, didn't read that. I suppose you meant this?

[edit]
Quote:In C++, array is spelled std::vector

lol, the wierd syntax of C++.

Regards,
jflanglois


Perfect! I got it working, thanks so much!!
-Conrad

This topic is closed to new replies.

Advertisement