这是代码,代码块指示错误在显示的行上:

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'`

最佳答案

问题出在第3个参数上,您尝试在其中传递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++ - 错误:从类型为Vector2D的右值对类型为Vector2D&的非常量引用进行了无效的初始化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26116315/

10-09 06:00