-3

I want to have an array of sf::Texture's(sfml) in a class, and then want to have a function that returns a pointer to that specific item in the array(vector). I then want to pass that to sprite.SetTexture(also SFML) which requires a sf::texture. But I can't get it to work, this is what I currently have, I left unimportant things out:

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.
    }
};

and this is the function defenition of Sprite::setTexture:

void setTexture(const Texture& texture, bool resetRect = false);

And the error it gives i have put as a comment in the code(next to the setTexture function) So what I want is, that instead of making a copy of the vector item to the texture var in the DrawSprite class, I want the *texture in DrawSprite to point to the item in the vector. But I do want to set the *texture in DrawSprite first as i want to use it for other things too. So i want to be able to supply the setTexture function with my *texture, and have it read the sf::Texture in the vector in SpriteTextures class.(Instead of reading a copy of it in the texture variable in the DrawSprite class itself.

SO if someone understands what I just said, can someone help me getting it work correctly?

Thanks!

  • Try to get it working with `int[]`, before you try to get it working with complex classes. – Beta Mar 17 '18 at 16:35
  • 1
    If you don't know how to get a reference from a pointer, then perhaps you should read a C++ book before getting into third party libraries like SFML. This is very basic information. – Benjamin Lindley Mar 17 '18 at 16:35
  • I can' t get it to work, thats why i'm asking. –  Mar 17 '18 at 16:37
  • The quick fix here is to return a reference from your `GetSecondTexture` function instead of a pointer. Since `setTexture` takes a reference. – Carl Mar 17 '18 at 16:38
  • @Benjamin Lindley i know how to get a reference, but i don't know how to combine it with the sfml setTexture. –  Mar 17 '18 at 16:38
  • It seems you are using `texture` as a local variable. If it is so, then declare and use a local variable. – 273K Mar 17 '18 at 16:38
  • @Carl, you mean like this: sf::Texture &GetSecondTexture() ? –  Mar 17 '18 at 16:39
  • `sprite.setTexture(*texture, true);` Am I missing something obvious here? – Stephen Newell Mar 17 '18 at 16:40
  • @S.M. I am using it in other functions in the class too, not shown because it is not relevant. –  Mar 17 '18 at 16:40
  • @Stephen Newell, yes, it doesnt take a pointer –  Mar 17 '18 at 16:41
  • 1
    Read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) – Passer By Mar 17 '18 at 16:41
  • @TimLeijten - Note the asterisk before `texture` in my comment. – Stephen Newell Mar 17 '18 at 16:42
  • 1
    @TimLeijten: No, you obviously don't know how to get a reference from a pointer, otherwise you would have recognized that Stephen's comment does exactly that. – Benjamin Lindley Mar 17 '18 at 16:42

1 Answers1

1

Basically, in this function:

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.
}

You're passing an sf::Texture* to sprite.setTexture(..), whereas in reality, it takes an sf::Texture&. Simply change the call to:

sprite.setTexture(*texture, true);

Or alternatively, make your other function simply return a reference:

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

Which means you're now returning what the sprite needs by default. Of course, you must ensure that your texture container stays alive for at least as long as your sprite, otherwise the Texture reference will no longer be valid.

EDIT

Also, looking at the comments, it seems you are mildly confused about the * operator and the & operator. Here is a quick snippet to explain the various use cases of those operators:

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.
Carl
  • 2,057
  • 1
  • 15
  • 20
  • Thanks, didn't know it was this easy. Will go and get a c++ book from the library to learn some pointers. –  Mar 17 '18 at 16:44
  • The second alternative wouldn't fix the problem. It would simply require him to take the address of the return value in order to store it in the `texture` variable. – Benjamin Lindley Mar 17 '18 at 16:46
  • @TimLeijten Also check the updated edit to see the extra syntax explanations. – Carl Mar 17 '18 at 16:46
  • @BenjaminLindley The second alternative would also require a re-design of that class, yes. Storing a reference or not storing it at all. Since this is a minimal example I decided to include an alternative, since I don't know how the full program looks. – Carl Mar 17 '18 at 16:47
  • Thanks, well what would be smarter to do? Have my function returna pointer, or a reference? –  Mar 17 '18 at 17:07
  • It turned out to not be just the pointers, I had 2 .h files include each other, which caused errors, and is what made me think my classes and pointer usages where worng. –  Mar 17 '18 at 18:50