I´m new to C++ and have researched about arrays, pointers and so on, but I can´t figure out how to make this work. Hope you can help!
I need an two dimensional array frases that contains 3 x 2 objects MGFrase. They will be retrieved by a method getFrase so I can use MGFrase´s method setTone to change it´s property altura, which is an array of ints.
In the code below, I expected to change alturas[0] of frases[1, 0] but it keeps changing frases[0, 0].
Is this the right way to create the array? Is there a problem in the methods?
If my question is not clear enough or isn´t on par with the forum´s rules please let me know so I can edit it, I´m new here.
Thank you all in advance!
MGComposition::MGComposition(){
MGFrases** frases;
frases = new MGFrase* [3];
for( int n = 0 ; n < 3 ; n ++ ){
frases [n] = new MGFrase[2];
}
for( int n = 0 ; n < 3 ; n ++ ){
frases[n, 0] = new MGFrase();
frases[n, 1] = new MGFrase();
}
}
MGFrase* MGComposition::getFrase( int channel , int numFrase ){
return frases[ channel, numFrase] ;
}
void MGComposition::log(){
cout << "- Composition --\n";
for( int n = 0 ; n < 3 ; n ++ ){
frases[ n , 0]->log();
frases[ n , 1]->log();
}
}
MGFrase::MGFrase(){
int alturas [10];
}
void MGFrase::setTone(int tone, int index) {
alturas[index] = tone;
}
void MGFrase::log() {
cout << "\n\nLog de MGFrase\n";
cout << "\nAlturas \n";
for (int nota = 0; nota < numNotes; nota++) {
cout << alturas [nota] << ", ";
}
}
MGGenerator::MGGenerator() {
MGComposition* composition;
composition = theComposition;
MGFrase* fraseDoBaixo;
fraseDoBaixo = composition->getFrase(1, 0);
fraseDoBaixo->setTone(8, 0);
composition->log();
}
PS: This is how it is now with the solution:
MGFrase*** frases;
frases = new MGFrase** [3];
for( int n = 0 ; n < 3 ; n ++ ){
frases [n] = new MGFrase*[2];
}
for( int n = 0 ; n < 3 ; n ++ ){
frases[n][0] = new MGFrase();
frases[n][1] = new MGFrase();
}
frases[ n , 0];C++ is not Delphi or Pascal. A multidimension array in C++ requires separate[ ]for each dimension.