PDA

View Full Version : [C++] Puntatori a Matrici


bobby1112
26-05-2010, 14:05
salve raga come da titolo devo restituire un matrice di char dichiarata in questo modo

file .h
#ifndef MAP_H
#define MAP_H

#define MAX_SIZE 36
class Map {

private :

static const char earth[MAX_SIZE][MAX_SIZE];
const char** earthPtr;

public :

void printEarth();
const char** getEarth();

};
#endif


file .cpp
#include <iostream>
#include "Map.h"
using namespace std;

const char Map::earth[36][36]={

/*0*/ "************* ********************",
/*1*/ "* * * *",
/*2*/ "* * * *",
/*3*/ "* ** * ** *",
/*4*/ "* ** * ** *",
/*5*/ "* * * *",
/*6*/ "* *** * * *",
/*7*/ "* * * * ****** *",
/*8*/ "* * * * *",
/*9*/ "* * * * *",
/*10*/ "* * ********** *",
/*11*/ "* * * *",
/*12*/ "* * *",
/*13*/ "* *",
/*14*/ "* * * * *",
/*15*/ "***********************************",
// 01234567890123456789012345678901234
};





void Map::printEarth()
{
for(int i=0;i<=36;i++)
{
cout<<endl;
for(int j=0;j<36;j++)
cout<<earth[i][j];


}
}

const char** Map::getEarth()
{
earthPtr=&earth[0][0];
return earthPtr;

}


earthPtr=&earth[0][0]; in questa istruzione mi da questo errore

impossibile convertire da 'const char *' a 'const char **'

bobby1112
26-05-2010, 19:56
up

wingman87
26-05-2010, 20:15
E' giusto, earth[x][y] è un char, quindi &earth[x][y] è un puntatore a char, non un doppio puntatore.

bobby1112
01-06-2010, 07:37
ma se poi vado a richiamare la funzione


const char* Map::getEarth()
{
earthPtr=&earth[0][0];
return earthPtr;
}


e la utilizzo in un main tipo


Map gameMap;
char* matPtr=gameMap.getEarth();

cout<<matPtr[5][5];



sul cout mi da un errore di puntatore

TRF83
01-06-2010, 11:32
Map gameMap;
char* matPtr=gameMap.getEarth();

cout<<matPtr[5][5];

prova con
cout<<(*matPtr)[5][5]
matPtr è un puntatore che non ha la struttura di "doppio array". Questa struttora ce l'ha la memoria "puntata da" matPtr