【问题标题】:How do I fix "initial value of reference to non-const must be an lvalue?"如何修复“对非常量的初始引用值必须是左值?”
【发布时间】:2019-06-14 02:19:46
【问题描述】:

我一直在研究 Hilze Vonck 的 YouTube 系列,介绍如何使用 SFML 库开发游戏。我接近本系列的结尾,正在介绍碰撞检测。据我了解,Vonck 使用了一个名为“Collider”的类,他为每个要检查碰撞的对象添加了一个 Collider。长话短说,Collider 函数经常使用 & 引用,我承认我并不总是很清楚发生了什么。

所以我得到了错误“引用非常量的初始值必须是左值”。首先,我研究了这个错误的含义。据我了解,当函数不要求 const 时,我传递了一个 const。但是,我不知道如何解决它。

仍然希望自己解决问题,重新学习了如何传递引用和如何传递指针,但仍然无法弄清楚。

视频中的 cmets 中的人也遇到了同样的问题。我相信 Vonck 使用的是旧版本的 Visual Studio 和旧版本的 SFML。

编辑:我尝试稍微修剪一下我的代码。如果我还需要删减一些代码,请告诉我。

另外,如果您想观看我引用的特定视频,请点击此处:https://www.youtube.com/watch?v=l2iCYCLi6MU&t=442s

main.cpp (red-squiggly-error 在 player.GetCollider() 下)

#include <SFML\Graphics.hpp>
#include "Player.h"
#include "Platform.h"

Player player(&playerTexture, sf::Vector2u(3,9), 0.3f, 100.0f);

Platform platform1(nullptr, sf::Vector2f(400.0f, 200.0f), sf::Vector2f(500.0f, 200.0f));
Platform platform2(nullptr, sf::Vector2f(400.0f, 200.0f), sf::Vector2f(500.0f, 0.0f));

player.Update(deltaTime);

platform1.GetCollider().CheckCollision(0.0f, player.GetCollider());
platform2.GetCollider().CheckCollision(0.0f, player.GetCollider());

对撞机.h

#pragma once
#include <SFML/Graphics.hpp>

class Collider
{
public:
    Collider(sf::RectangleShape& body);
    ~Collider();

    void Move(float dx, float dy) { body.move(dx, dy); }

    bool CheckCollision(float push, Collider & other );
    sf::Vector2f GetPosition() { return body.getPosition(); }
    sf::Vector2f GetHalfSize() { return body.getSize() / 2.0f; }


private:

    sf::RectangleShape& body;
};

对撞机.cpp

#include "Collider.h"



Collider::Collider(sf::RectangleShape& body) :
    body(body)
{
}

bool Collider::CheckCollision(float push, Collider & other)
{
    //check collision
}

播放器.h

#pragma once
#include <SFML/Graphics.hpp>
#include "animation.h"
#include "Collider.h"

class Player
{
public:
    Player(sf::Texture* texture, sf::Vector2u imageCount, float switchTime, float speed);
    ~Player();

public:
    void Update(float deltaTime);
    sf::Vector2f getPosition() { return body.getPosition(); }

    Collider GetCollider() { return Collider(body); }

private:
    sf::RectangleShape body;
};

播放器.cpp

#include "Player.h"

Player::Player(sf::Texture* texture, sf::Vector2u imageCount, float switchTime, float speed) :
    animation(texture, imageCount, switchTime)
{
}

平台.h

#pragma once
#include <SFML/Graphics.hpp>
#include "Collider.h"

class Platform
{
public:
    Platform(sf::Texture* texture, sf::Vector2f size, sf::Vector2f position);
    ~Platform();

    void Draw(sf::RenderWindow& window);
    Collider GetCollider() { return Collider(body); }

private:
    sf::RectangleShape body;

};

平台.cpp

#include "Platform.h"

Platform::Platform(sf::Texture* texture, sf::Vector2f size, sf::Vector2f position)
{
}

欢迎任何帮助。

【问题讨论】:

  • "我包含了完整的类文件";不,您应该只包含minimal reproducible example,绝对最少的代码量,最好是一块,任何人都可以简单地剪切和粘贴并重现您的错误。此外,您没有指出哪个特定文件/行会导致有问题的编译错误。难道你不认为如果你只是指出编译错误发生在哪里,那么更有可能有人可以简单地查看代码并告诉你原因,而不是要求有人实际编译整个东西才能找到错误所在是,首先,然后才弄清楚?
  • 我会尽量减少代码。在主要代码之前,我提到了错误在哪里。这还不够好吗?
  • 这还不够“好”,仅仅是因为发布的信息量很大,而且大部分信息都无关紧要。更容易发现相关的信息位,因为它几乎是所有显示的,而不是分散在大量几乎不相关的细节中的东西。这正是stackoverflow.com 的help center 解释需要minimal reproducible example 的原因。现在问题已经很明显了……

标签: c++ collision-detection sfml


【解决方案1】:

这里出现编译错误:

platform1.GetCollider().CheckCollision(0.0f, player.GetCollider());

GetCollider() 返回一个Collider 对象。此表达式的“player.GetCollider()”部分是 临时 Collider 值。 CheckCollision 是这样声明的:

bool Collider::CheckCollision(float push, Collider & other)

C++ 禁止将临时对象作为非常量引用参数传递。最简单的解决方法是简单地将临时对象存储在某个地方,首先:

Collider c=player.GetCollider();

platform1.GetCollider().CheckCollision(0.0f, c);

其他类似的调用也需要修复。但更合适的解决方法是将参数更改为 const 引用:

bool Collider::CheckCollision(float push, const Collider &other)

这将允许为这个参数直接传递一个临时值。

请注意,这将需要对所示代码进行进一步更改。从CheckCollision() 调用的other 方法现在必须是const 类方法。

实际做这项工作是值得的,因为它会教你一些相当基本但重要的概念。类方法通常分为两类:不修改类实例的方法和修改类实例的方法。那些不应该声明为const。这允许您的编译器在您不应该修改类实例时发现它。

【讨论】:

  • 谢谢。正如你所建议的,我将努力发展我对课程的理解。
猜你喜欢
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-15
  • 2021-06-15
相关资源
最近更新 更多