Function error.

Hello,
I have made a function which looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void MapRead(int ** a, int rows, int cols, int level){
stringstream ss;
ss << level << ".aem";
ifstream LoadMap(ss.str().c_str());
LoadMap >> rows >> cols;
	// allocation
a = new int*[rows];
for(int i = 0; i < rows; i++){
		a[i] = new int[cols];}
// initialization
for(int j = 0; j < rows; j++){
	for(int i = 0; i < cols; i++){
		LoadMap >> a[j][i];}}
LoadMap.close();
}

To load a file which looks like this (1.aem):
5 5 1 0 2 1 0 4 1 1 1 0 0 1 5 0 6 6 1 0 3 8 0 4 1 1 7

When I compile the program, it runs fine (even this function), but when I try to
access the input from it (which could be just as simple as printing it out), it crashes.

I know that the function is in perfect working order, as I copied the contents of the function from the file over to the main file and the program run as intended, but when it is in the form of a separate function, it doesn't work.

How would I get around this? Thanks for your time!
void MapRead(int ** a, int rows, int cols, int level); a, rows, cols are dummy, as they are passed by copy but you don't use their value.

Please use a container (like std::vector)
Maybe, try:
 
void MapRead(int ** & a, int & rows, int & cols, int level)

Like ne555 said, don't do value. Do reference.
Thankyou both.
void MapRead(int ** & a, int & rows, int & cols, int level)

I tried this. The compiler came up with an error after doing this that said "error: expected initialization before 'namespace'". Is there any way around this, or will std:: vector be a better option. I understand a little bit about vectors, but I am not greatly experienced with them. Could someone post me an example of how to use them, please?
This works for me with your file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;

void MapRead(int  ** & a, int & rows, int & cols, int level){
   stringstream ss;
   ss << level << ".aem";
   ifstream LoadMap(ss.str().c_str());
   LoadMap >> rows >> cols;
           // allocation
   a = new int*[rows];
   for(int i = 0; i < rows; i++){
       a[i] = new int[cols];
   }
   // initialization
   for(int j = 0; j < rows; j++){
      for(int i = 0; i < cols; i++){
           LoadMap >> a[j][i];
      }
   }
   LoadMap.close();
}

int main()
{
   int **a(0),c(0),r(0);
   MapRead(a,r,c,1);
   cout << "a: " << a << ", c: " << c << ", r: " << r << endl;
   cout << a[0][0] << "," << a[1][1] << endl;
   // Edit:  Adding the delete
   if ( a ) {
       for (int i(0); i < r; i++ ) {
           delete [] a[i];
     }
     delete [] a;
   } 
   return 0;
}

Ouptput

./a.out
a: 0x9852220, c: 5, r: 5
1,1
Last edited on
int **&a yuck.
error: expected initialization before 'namespace'
I don't see any namespace in your code...

1
2
3
4
5
6
7
8
9
vector< vector<int> > MapRead(int level){
  //...
  vector< vector<int> > Map( rows, vector<int>(cols) ); //construct the rows \times cols matrix
  for(size_t K=0; K<rows; ++K)
    for(size_t L=0; L<cols; ++L)
      file >> Map[K][L];

  return Map;
}
Sure, but try the vector code and if that works use it. Whatever works!
Thankyou very much. This helped well!
Topic archived. No new replies allowed.