Le code C suivant donne une erreur:
incompatible types in assignment of 'int' to 'int [(((sizetype)(((ssizetype)m) -1)) 1)]'
Sur les lignes où j’appelle les fonctions récursives:
int mergeSort(int arr[], int n) { if (n > 1) { int m = (int)(n / 2); int A1[m]; // array 1 --> 1 TO M int A2[nm]; // array 2 --> M + 1 TO N int temp = m+1; for (int i = 0; i < n; i++) { if (i == m) break; A1[i] = arr[i]; A2[i] = arr[temp++]; } A1 = mergeSort(A1, m); //error happens here, assigning an //integer into an integer array. A2 = mergeSort(A2, n - m); arr = merge(A1, A2); } return arr; }
Que signifie cette erreur?
Il y a plusieurs erreurs:
A1[m]
) – utilisez plutôt std::vector
. A1 =...
est illégal int
. mergeSort
renvoie un int
(pourquoi?) Vos problèmes sont principalement liés au fait que les tableaux de style C sont cassés et ne doivent pas être utilisés. Il suffit de remplacer tous les tableaux de style C par std::vector
et vos problèmes devraient disparaître. À l’exception de la valeur de retour, qui (du sharepoint vue des choses) devrait également être std::vector
.
Programme Barebones pour produire l’erreur ci-dessus:
#include using namespace std; int main(){ int (*p)[ 5 ]; //p is "a pointer to an array of 5 ints". p[0] = 10; //Try to assign the integer 10 to array int[5]? NO! //You get: //error: incompatible types in assignment of `int' to `int[5]' }
Vous ne pouvez pas assigner un int à un tableau d’ints. Vous devez d’abord le déréférencer comme ceci:
#include using namespace std; int main(){ int (*p)[ 5 ]; //p is "a pointer to an array of 5 ints". *p[0] = 10; //assign the integer 10 to position 0 of the //array where p is pointing. cout << *p[0]; //prints 10. no error thrown. }