SFML,C ++如何访问动态分配的sf :: VertexArray中的特定sf :: Vertex?

问题描述

我是SFML的新手,正在C ++中学习它,但有这个我无法解决的问题。
我的程序(类)包含的内容是:

在头文件中:

sf::VertexArray *hitbox;

在源文件中:

this->hitbox = new sf::VertexArray(sf::TriangleFan,20); //example

有一种方法

void Object::setPosition(sf::Vector2f position)
{
    if(this->hitbox->getVertexCount()!=0)
    {
        this->hitbox->position = position; //error here
    }
}

编译器这样说:

 error: 'class sf::VertexArray' has no member named 'position'
   this->hitbox->position = position;
                 ^

所以问题是我想更改第一个顶点的位置,但是当我动态分配sf::VertexArray时似乎无法访问它。我在https://www.sfml-dev.org/上读到,sf::VertexArray实际上是std::vector<sf::Vertex>,带有[]运算符重载,因此应该可以做到这一点,但是我很挣扎找到它。此外,该类不会继承自sf::Transformable。我该如何解决这个问题?

编辑:
this->hitbox[0]->position = position;

this->hitbox[0].position = position;
不要解决这个问题。在第一种情况下,编译器遇到的问题是hitbox [0]不是指针,在第二种情况下,它与上述错误相同/

解决方法

由于first_program.py是指向this->hitbox的指针,因此您需要在使用sf::VertexArray之前取消引用它:

operator[]

最好根本不使用指针。

,

按如下方式使用重载的[]运算符:

this->hitbox[0].position = position;

这将索引顶点的第0个条目,您将可以根据需要对其进行修改。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...