Riprendo questa discussione perchè ho l'identico problema che avevo postato tempo fa.. come sempre ho una matrice dinamica e quando cerco di cambiare un suo elemento o non ne cambia nessuno o ne cambia due(dei quali nesssuno dei due è quello aspettato).
Ecco l'output:codice:#include <iostream> using namespace std; class Immagine{ int** foglio; int altezza, larghezza; public: Immagine(int,int); ~Immagine(); bool set(int,int); friend ostream& operator<<(ostream&, const Immagine&); }; Immagine::Immagine(int la, int al) { altezza = al; larghezza = la; foglio = new int*[larghezza]; for(int i=0; i<larghezza; i++) { foglio[i] = new int[altezza]; for(int j=0; j<altezza; j++) foglio[i][j] = 0; } cout << "Costruttore\n"; } Immagine::~Immagine() { for(int i=0; i<larghezza; i++) delete[] foglio[i]; delete[] foglio; cout << "Distruttore\n"; } bool Immagine::set(int la, int al) { if(la>larghezza || la<0) return false; if(al>altezza || la<0) return false; foglio[la][al] = 1; return true; } ostream& operator<<(ostream& os, const Immagine& im) { for(int i=0; i<im.altezza; i++) { for(int j=0; j<im.larghezza; j++) { if(im.foglio[i][j] == 0) os << im.foglio[i][j]; else os << im.foglio[i][j]; } os << endl; } return os; } int main(){ Immagine i(15,8); i.set(2,2); cout << i; return 0; }
codice:The Debugger has exited with status 0. [Session started at 2009-01-30 12:16:54 +0100.] Costruttore 000000000000000 000000000010000 001000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 Distruttore The Debugger has exited with status 0.

Rispondi quotando