2015-02-11 62 views
-4
bool sortingGame(Player Player1, Player Player2) 
{ 
    if (Player1.gamePercent() > Player2.gamePercent())// first compare precetage (float) 
    { 
     return true; 
    } 
    else if (Player2.gamePercent() > Player1.gamePercent()) 
    { 
     return false; 
    } 
    else if (Player1.getLastName() > Player2.getLastName())//then names (std::string) 
    { 
     return true; 
    } 
    else if (Player2.getLastName() > Player1.getLastName()) 
    { 
     return false; 
    } 
    else if (Player1.getFirstName() > Player2.getFirstName()) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 

繼承人++時無效操作者<:下,使用在main()排序

排序(Players.begin(),Players.end(),sortingGame);

這是在Visual Studio中調試時顯示的錯誤。

Program:C:\.....\include\algorithm 
Line:3014 
Expression: invalid operator< 
+2

什麼行是3014,究竟是什麼? – Dai 2015-02-11 01:29:16

+1

不確定...它在#include PinesF 2015-02-11 01:33:43

+0

'main()'迭代器中是'Player1'和'Player2'嗎? – ikh 2015-02-11 01:36:32

回答

1

當我模擬玩家類並運行你的代碼時,它可以正常工作。錯誤必須屬於別的地方。

但是你的代碼有可能大幅收緊,通過檢查相等,然後返回比較的結果:

bool sortingGame(Player Player1, Player Player2) 
{ 
    if (Player1.gamePercent() != Player2.gamePercent())// first compare precetage (float) 
    { 
     return Player1.gamePercent() > Player2.gamePercent(); 
    } 
    else if (Player1.getLastName() != Player2.getLastName()) 
    { 
     return Player1.getLastName() > Player2.getLastName(); 
    } 
    else 
    { 
     return Player1.getFirstName() > Player2.getFirstName(); 
    } 

} 

考慮,以及該字符串在反向排序(按字母順序排列),通常上市。因此,小於運營商(<)會更好地工作:

bool sortingGame(Player Player1, Player Player2) 
{ 
    if (Player1.gamePercent() != Player2.gamePercent())// first compare precetage (float) 
    { 
     return Player1.gamePercent() > Player2.gamePercent(); 
    } 
    else if (Player1.getLastName() != Player2.getLastName()) 
    { 
     return Player1.getLastName() < Player2.getLastName(); 
    } 
    else 
    { 
     return Player1.getFirstName() < Player2.getFirstName(); 
    } 

}