0

This is kind of a strange question but I'm not getting much useful info from the compiler and am wondering what I'm doing wrong.

I have a program (we'll just call it 'main') that includes a class 'world', which itself includes a class 'tile'. Everything is being included correctly as far as I can tell - I can add an instance of tile as a member of an instance of world and then (via public getter methods) access the data in the tile from main.

The problem occurs when I want to use a multi-dimensional array of tiles as a member of an instance of world - the compiler crashes with the helpful message "cc1plus.exe has stopped working" and the helpful text "[Build Error] [main.o] Error 1".

I have a parameterless default constructor defined for the tile class (and for the world class as well although I don't think that's relevant.) I'm not sure at all what else could cause this. Any insight would be really appreciated.

Ex: main.cpp

#include <world.h>
world newWorld;
int main(int argc, char *argv[]){
newWorld = world();
}

world.h

#include <tile.h>
class world {
tile wmap[100][100][10]
public:
world(){
//code that calls the parametized constructor for tile for each element in the array
}
};

tile.h

class tile {
int temp;
public:
tile(){}
tile(int t){temp = t;}
};

I apologize if the answer should be obvious, this is my first foray back into C++ in several years and it seems I've gotten sloppy.

Ryoshi
  • 113
  • 7
  • Have you tried a smaller array? Perhaps you're hitting an internal compiler limit. It seems the limit would be bit low if you are, but worth trying. – Dark Falcon Sep 19 '14 at 18:56
  • Works for me: http://coliru.stacked-crooked.com/a/423b651b6aff8c8d – Csq Sep 19 '14 at 19:28

1 Answers1

0

For a multi-dimensional array in C++ you need to allocate the space, before you try to use it, in this case probably using new().

See How do I declare a 2d array in C++ using new?

Community
  • 1
  • 1
Lumi
  • 186
  • 1
  • 8
  • Hmm - would that cause the compiler itself to bomb out though? It seems like that should cause a build error. Plus, can you really instantiate private class members in a loop like that? – Ryoshi Sep 19 '14 at 21:15
  • And I apologize for a further dumb question but I really want to understand WHY it's not working rather than just what's going wrong: I coded a program to generate random roguelike-style dungeons a while back and this program was meant to clean up and properly encapsulate some of that code - in that case, my map was basically a few multi-dimension char arrays, and I didn't have to do anything specific to get them to work. Is that just because that was a basic type and this is a class? For whatever reason I feel like it should be working the same. Thanks for the help, though! – Ryoshi Sep 19 '14 at 21:15
  • I don't have a windows compiler to test this on, speculating ( agree it's strange that the compiler is bombing out) - it could be: `world newWorld;` since that will statically create an instance which could then invoke the constructor. As to why. Multi-dimensional arrays weren't part of the C/C++ standard, and K&R's example of how to do them actually used malloc. As far as C++ is concerned, it's all just memory - so with something like char c[10][10], the area of memory allocated is enough that you might get away with it, if you were lucky with the memory that ended up being overwritten. – Lumi Sep 19 '14 at 23:12