gpt4 book ai didi

c++ - 如何修复 "initial value of reference to non-const must be an lvalue?"

转载 作者:行者123 更新时间:2023-11-30 04:52:38 26 4
gpt4 key购买 nike

我一直在研究 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)
{
}

欢迎任何帮助。

最佳答案

这里出现编译错误:

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。这允许您的编译器发现您不小心修改了类实例。

关于c++ - 如何修复 "initial value of reference to non-const must be an lvalue?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54277329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com