首页 > 解决方案 > 如何修复“对非常量的初始引用值必须是左值?”

问题描述

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

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

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

视频评论中的人也有同样的问题。我相信 Vonck 使用的是旧版本的 Visual Studio 和旧版本的 SFML。

编辑:我试着稍微修剪一下我的代码。如果我仍然需要删除一些代码,请告诉我。

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

main.cpp(红色波浪错误在 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)
{
}

欢迎任何帮助。

标签: c++collision-detectionsfml

解决方案


编译错误发生在这里:

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)

对于此参数,这将允许直接传递一个临时值。

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

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


推荐阅读