Hello! I'm working on this roguelike and I'm trying to make it so that the different "maps" can be loaded from text files. It was going pretty good until I tried to get it to return the filled array. Take a look:

void getMap(string str[10][10]){
  ifstream myfile("world1.txt");
    int a = 0;
    int b = 0;
 
    while(!myfile.eof())
    {
      getline(myfile,str[a][b],' ');
      if(a ==9)
      {
           a=0;
           ++b;
           getline(myfile,str[a][b],' ');
      }
      a++;
    }
}


int main(){
	 string map[10][10];
	 getMap(&map);


	 system("PAUSE");
	 return 0;
}

So I'm trying to copy the completed array "str" at the end of function "getMap" into array "map". I would've thought this code would work, but when I try to compile I get "error C2664: 'getMap' : cannot convert parameter 1 from 'std::string (*)[10][10]' to 'std::string [][10]'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

Any suggestions?

>Any suggestions?
Sure. Change getMap(&map) to getMap(map) . You're adding an unnecessary level of indirection, and the compiler doesn't like it.

p.s. For the record, while(!myfile.eof()) is a bad idea. This describes the equivalent problem in C.

Woah! That worked great, but I don't understand how.
How is MAP being changed when getMap returns nothing and has no reference parameters?

Map is declared as an array. When arrays are passed as parameter to a function they are passed by reference by definition of the language. Being passed by reference means that any changes to the contents of the array within the function will remain in the array once the function that did the changing exits. This is unlike other types such as int, double, string, or user derived types where you do need to specifically pass the information by reference to have the same trick happen. You will need to read the language specification or wait for Narue or one of the other gurus to tell why it was set up this way. All I know is that it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.