这是代码,CodeBlocks 指出错误在 shwon 行:
bool SoccerTeam::isPassSafeFromOpponent(Vector2D from,
Vector2D target,
const PlayerBase* const receiver,
const PlayerBase* const opp,
double PassingForce)const
{
Vector2D ToTarget = target - from;
Vector2D ToTargetNormalized = Vec2DNormalize(ToTarget);
Vector2D LocalPosOpp = PointToLocalSpace(opp->Pos(),
ToTargetNormalized,
ToTargetNormalized.Perp(),
from); // *** ERROR ***
错误信息:
error: invalid initialization of non-const reference of type 'Vector2D&'
from an rvalue of type 'Vector2D'`
最佳答案
问题出在第三个参数上,您在其中尝试传递 Vector2D::Perp() 函数的返回值作为引用。 (这同样适用于第一个参数,但我猜这是一个 const Vector2D&,所以它可能不会哭。)尝试以下操作:
bool SoccerTeam::isPassSafeFromOpponent(Vector2D from,
Vector2D target,
const PlayerBase* const receiver,
const PlayerBase* const opp,
double PassingForce)const
{
Vector2D ToTarget = target - from;
Vector2D ToTargetNormalized = Vec2DNormalize(ToTarget);
Vector2D ToTargetNormalizedPerp = ToTargetNormalized.Perp();
Vector2D LocalPosOpp = PointToLocalSpace(opp->Pos(),
ToTargetNormalized,
ToTargetNormalizedPerp,
from);
关于c++ - 错误 : invalid initialization of non-const reference of type 'Vector2D&' from an rvalue of type 'Vector2D' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26116315/