Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Explicitly initializing sprite  (Read 479 times)

0 Members and 1 Guest are viewing this topic.

Sweetwine

  • Newbie
  • *
  • Posts: 3
    • View Profile
Explicitly initializing sprite
« on: July 05, 2023, 05:38:07 am »
Hello,

I'm working through a book and trying to create a Game class in a header file which has a sprite as a private variable.  I am getting the error "Constructor for 'Game' must explicitly initialize the member 'm_mushroom' [name of sprite] which does not have a default constructor" when I try to compile and can't seem to figure out why.  I've tried initializing the sprite inside the default constructor by setting the texture, but this apparently doesn't work.  Here is my code for the constructor:

    Game(): m_window("Chapter 2", sf::Vector2u(800,600)) {
        m_mushroomTexture.loadFromFile("path_to_mushroom_image_here");
        m_mushroom.setTexture(m_mushroomTexture);
        m_increment = sf::Vector2i(4,4);
    }

This is with Xcode on macOS.  Any ideas on what I'm doing wrong?  Thanks.

Sweetwine

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Explicitly initializing sprite
« Reply #1 on: July 05, 2023, 05:44:15 am »
Disregard.  I was able to include the sprite in the initialization list before loading the texture and it worked.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Explicitly initializing sprite
« Reply #2 on: July 05, 2023, 04:51:05 pm »
'm_mushroom' [name of sprite] which does not have a default constructor
Sprite ()
    Default constructor.

Can we presume you are using SFML 3 (the master branch) already as it seems that has removed ability to default construct a sprite?

I mention this only to clarify for anyone wondering why it's happening when it's in the documentation; the documentation is not for SFML 3.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Sweetwine

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Explicitly initializing sprite
« Reply #3 on: July 05, 2023, 06:12:01 pm »
This was actually occurring with version 2.6.0.  I reverted back to 2.5.1 and the error disappeared without having to include the sprite in the initialization list.  Not sure what the problem was.  I also switched over to CLion for my IDE, though I don't think that made any difference because I was still getting the error in CLion with 2.6.0.

 

anything