Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何正确使用带函数和返回的指针?_C++_Function_Pointers_Vector_Sfml - Fatal编程技术网

C++ 如何正确使用带函数和返回的指针?

C++ 如何正确使用带函数和返回的指针?,c++,function,pointers,vector,sfml,C++,Function,Pointers,Vector,Sfml,我希望在一个类中有一个sf::Texture(sfml)数组,然后希望有一个函数返回指向数组(vector)中特定项的指针。 然后我想把它传递给sprite.SetTexture(也是SFML),它需要一个sf::纹理。 但我不能让它工作,这是我目前拥有的,我忽略了不重要的事情: class SpriteTextures { public: std::vector<sf::Texture> Textures; //code to fill the vector he

我希望在一个类中有一个sf::Texture(sfml)数组,然后希望有一个函数返回指向数组(vector)中特定项的指针。 然后我想把它传递给sprite.SetTexture(也是SFML),它需要一个sf::纹理。 但我不能让它工作,这是我目前拥有的,我忽略了不重要的事情:

class SpriteTextures
{
public:
    std::vector<sf::Texture> Textures;

    //code to fill the vector here

    sf::Texture *GetSecondTexture()
    {
        return &Textures[1];
    }
};

class DrawSprite
{
    sf::Texture *texture;
    sf::Sprite sprite;

public:
    void SetSpriteTexture()
    {
        SpriteTextures SprTexs;
        texture = SprTexs.GetSecondTexture();
        sprite.setTexture(texture, true); // Gives error about no suitable constructor exists to convert from sf::Texture * to sf::Texture

        // do some other stuff with the texture here.
    }
};
它给出的错误我已经作为注释放在代码中(在setTexture函数旁边) 所以我想要的是,我希望DrawSprite中的*纹理指向向量中的项,而不是将向量项复制到DrawSprite类中的纹理变量。 但我确实想先在DrawSprite中设置*纹理,因为我也想将其用于其他用途。 因此,我希望能够为setTexture函数提供我的*texture,并让它在SpriteTextures类的vector中读取sf::texture(而不是在DrawSprite类本身的texture变量中读取它的副本)

所以,如果有人理解我刚才说的话,有人能帮我让它正常工作吗


谢谢!

基本上,在这个函数中:

void SetSpriteTexture()
{
    SpriteTextures SprTexs;
    texture = SprTexs.GetSecondTexture();
    sprite.setTexture(texture, true); // Gives error about no suitable constructor exists to convert from sf::Texture * to sf::Texture

    // do some other stuff with the texture here.
}
您正在将一个
sf::Texture*
传递给
sprite.setTexture(..)
,而实际上,它需要一个
sf::Texture&
。只需将调用更改为:

sprite.setTexture(*纹理,true);

或者,让其他函数简单地返回引用:

sf::Texture& GetSecondTexture()
{
    return Textures[1];
}
这意味着您现在返回的是精灵默认需要的内容。当然,您必须确保您的纹理容器至少与精灵一样长时间处于活动状态,否则纹理引用将不再有效

编辑

另外,看看这些注释,您似乎对
*
操作符和
&
操作符有点困惑。下面是一个快速片段,解释这些操作符的各种用例:

int a = 42;

int* b = &a; // * Declares pointer. & Takes address of a
int& c = a;  // & Declares reference.

*b = 32;     // * Dereferences the pointer, meaning you get the value it points to.
             // a, and c are now 32, and b points to a, which holds the value 32.

试着让它与
int[]一起工作,在你尝试让它与复杂的类一起工作之前。如果你不知道如何从指针中获取引用,那么也许你应该先读一本C++书籍,然后再进入SFML之类的第三方库。这是非常基本的信息。我不能让它工作,这就是我为什么要问的原因。读一句:“不,你显然不知道怎么弄。”一个指针的引用,否则你会知道史蒂芬的评论确实是这样的。谢谢,不知道这很容易。从图书馆得到一本C++书来学习一些指针。第二个选择不会解决这个问题。它只需要他把返回值的地址存储起来。在
texture
变量中。@TimLeijten还检查更新后的编辑以查看额外的语法解释。@BenjaminLindley第二个备选方案也需要重新设计该类,是的。存储引用还是根本不存储它。由于这是一个最小的示例,我决定包含一个备选方案,因为我不知道fu是如何实现的我的程序看起来很好。谢谢,那么什么更聪明呢?让我的函数返回指针或引用?
int a = 42;

int* b = &a; // * Declares pointer. & Takes address of a
int& c = a;  // & Declares reference.

*b = 32;     // * Dereferences the pointer, meaning you get the value it points to.
             // a, and c are now 32, and b points to a, which holds the value 32.