2013-04-24 33 views
0

我無法弄清楚如何讓排序選擇工作。我必須按升序從結構中排序分數(雙打)。使用排序選擇與C++中的結構數組

這是我的代碼,我會評論我在哪裏得到的錯誤。

我的結構:

struct diveInfo 
{ 
    string diversName; 
    double totalScore; 
    double totalScore; 
    double diff; 
    double scores[NUM_SCORES]; 
}; 

我的函數按升序對分數進行排序:

void selectionSort(diveInfo *ptr, int size) 
{ 
    diveInfo temp; 

    double minValue; 

    int startScan; 
    int minIndex; 

    for (startScan = 0; startScan < (size - 1); startScan++) 
    { 
     minIndex = startScan; 
     minValue = ptr[startScan].scores; //keep getting an error here saying type double cannot be assigned to an entity of type double. 
     temp = ptr[startScan]; 

     for (int index = startScan + 1; index < size; index++) 
     { 
      if (ptr[index].scores < minValue) 
      { 
       temp = ptr[index]; 
       minIndex = index; 
      } 

     } 
     ptr[minIndex] = ptr[startScan]; 
     ptr[startScan] = temp; 
    } 
} 
+0

分數是一個數組。你可能想對totalScore進行排序嗎?你確實需要考慮你在做什麼。應該很清楚,將雙精度數組分配給double是不可能的。 – john 2013-04-24 14:22:05

回答

1

scores是雙打的數組,你需要這個數組中指定索引來訪問特定的雙重價值。
例如minValue = ptr[startScan].scores[0];

0

您試圖將double *(雙精度數組是double *)賦值給double。

你可以嘗試改變你的雙打數組成vector的雙打。然後,您可以使用std::sortstd::stable_sort對它們進行分類。

1
minValue = ptr[startScan].scores; 

scores是雙數組。數組名也充當指針。 scoresdouble*的類型[正好可以指向一個大小爲NUM_SCORES的數組] 您正在爲int分配一個double指針。