我有以下类文件
#pragma once
#include <memory>
#include <iostream>
#include <SFML/Graphics.hpp>
class gif
{
public:
gif(const std::vector<std::shared_ptr<sf::Texture>>& textures);
sf::Texture getAt(int);
private:
std::vector<std::shared_ptr<sf::Texture>> textures;
};
gif::gif(const std::vector<std::shared_ptr<sf::Texture>>& c)
{
textures = c;
}
sf::Texture& gif::getAt(int index)
{
return textures.at(index);
}
可变纹理似乎不像传统 vector 那样工作,也没有 at(int)
函数指向我的 vector 中的元素。我如何才能使用 integer
指向 textures
中的某个 sf::Texture
。
我试过在谷歌上搜索,但似乎找不到任何可以帮助我解决这个问题的东西。我只是没有正确理解 std::shared_ptr
吗?如果我不是那么我将如何使用它。
最佳答案
variables.at()
将返回类型为 std::shared_ptr<sf::texture>
的对象, 它不会隐式转换为 sf::texture&
.您需要使用 operator*
取消引用它:
sf::Texture& gif::getAt(int index)
{
return *(textures.at(index));
}
关于c++ - 从 shared_ptr 获取一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47404244/