Problem mit Arrayzeiger
-
Hi !
Ich will folgendes machen:
//hier fängt die Methode an... int mix_matrix[vector_dimension][number_of_patches]; //column vector merged in a matrix //mach hier was... return mix_matrix;Als Fehler erhalte ich in der Konsole:
g++ -c -pipe -fno-strict-aliasing -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o PatchGenerator.o PatchGenerator.cpp PatchGenerator.cpp: In member function ‘int* PatchGenerator::transform_patches_to_vectors_and_merge_to_matrix(std::vector<QImage*, std::allocator<QImage*> >*, int)’: PatchGenerator.cpp:26: error: cannot convert ‘int (*)[(((unsigned int)(((int)vector_dimension) - 1)) + 1u)][(((unsigned int)(((int)number_of_patches) - 1)) + 1u)]’ to ‘int*’ in return make: *** [PatchGenerator.o] Fehler 1
-
Hier ist der Methodenheader:
int* PatchGenerator::transform_patches_to_vectors_and_merge_to_matrix( vector<QImage*>* patches , int rect_size ) {
-
mix_matrix ist vom Typ int**, du willst aber einen int* zurückgeben...
-
Stimmt, jetzt hab ich es mal geändert:
int** PatchGenerator::transform_patches_to_vectors_and_merge_to_matrix( vector<QImage*>* patches , int rect_size ) { int mix_matrix[vector_dimension][number_of_patches]; //column vector merge in a matrix //mach hier was.... return mix_matrix; }Ich erhalte wieder den Fehler:
PatchGenerator.cpp: In member function ‘int** PatchGenerator::transform_patches_to_vectors_and_merge_to_matrix(std::vector<QImage*, std::allocator<QImage*> >*, int)’: PatchGenerator.cpp:22: error: cannot convert ‘int (*)[(((unsigned int)(((int)number_of_patches) - 1)) + 1u)]’ to ‘int**’ in return make: *** [PatchGenerator.o] Fehler 1
-
Hallo
Also auf deine Art kannst du kein dynamisches Array anlegen.
Mach es so:
int* a;
a = new int[x]; jetzt kann x auch eine Variable sein.Problem ist nur du kannst nicht:
int** a;
a = new int[x][y]; machen.Ich hab das mal so gelöst gehabt, aber ka mehr ob das ging^^:
int *a;
a = new int[x];
for(int i=0;i<x;i++)
{
a[i] = new int[y];
}Kannst ja mal probieren.
-
Nein das geht so leider nicht...
//zwar geht: int** mix_matrix = new int*[vector_dimension]; //das geht aber nicht: for( unsigned int matrix_column = 0 ; matrix_column < patches->size() ; matrix_column++ ) mix_matrix[matrix_column] = new int[gewuenschte_groesse];PatchGenerator.cpp:21: error: name lookup of ‘matrix_column’ changed for new ISO ‘for’ scoping
-
Okay ich habs so gelöst:
int** PatchGenerator::transform_patches_to_vectors_and_merge_to_matrix( vector<QImage*>* patches , int rect_size ) { int vector_dimension = rect_size * rect_size; int number_of_patches = patches->size(); int** mix_matrix = new int*[number_of_patches]; for( unsigned int matrix_column = 0 ; matrix_column < patches->size() ; matrix_column++ ) { int* vector = new int[vector_dimension]; for( int x = 0 ; x < rect_size ; ++x ) for( int y = 0 ; y < rect_size ; ++y ) vector[ x * rect_size + y ] = patches->at( matrix_column )->pixel( x , y ); mix_matrix[ matrix_column ] = vector; } return mix_matrix; }