c++ - 错误 : invalid initialization of non-const reference of type 'Vector2D&' from an rvalue of type 'Vector2D'

标签 c++ reference rvalue

这是代码,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/

相关文章:

c++ - std::线程问题

c++ - 我可以退回拷贝作为引用吗?

arrays - 在 Nim 中创建对数组的引用

c++ - 右值和左值之间的确切区别

C++ 右值引用构造函数对简单类型(Vector3、Matrix 等)的好处

c++ - 在 C++ 问题中链接 Cuda

c++ - GCC:如何找到不丢弃目标文件的原因

带有 boost asio 的 C++ 多路广播接收器

php - 在数组 var_dump 末尾添加 & 号

c++ - 返回*这个;删除指针