使用排序时 C++ 运算符 < 无效
C++ invalid operator< when using sorting
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(), 排序游戏);
这是在 Visual Studio 中调试时显示的错误。
Program:C:\.....\include\algorithm
Line:3014
Expression: invalid operator<
当我模拟播放器 class 和 运行 你的代码时它工作正常。该错误必须属于其他地方。
但是,您的代码可以通过检查相等性和 return 比较结果来大大加强:
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();
}
}
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(), 排序游戏);
这是在 Visual Studio 中调试时显示的错误。
Program:C:\.....\include\algorithm
Line:3014
Expression: invalid operator<
当我模拟播放器 class 和 运行 你的代码时它工作正常。该错误必须属于其他地方。
但是,您的代码可以通过检查相等性和 return 比较结果来大大加强:
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();
}
}