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

Author Topic: Problem with initialization of my variable to its class  (Read 163 times)

0 Members and 1 Guest are viewing this topic.

alaninho_08

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem with initialization of my variable to its class
« on: January 17, 2025, 06:03:54 am »
Hello. Initially, I was trying to separate my main.cpp to different classes for each player for my game in SFML 3.0.0 I'm trying to build, but when trying to assign a local variable to my class the compiler gives me an error saying "Impossible to make reference to a deleted function" image showing the error description.

So, I tried using including my class name in the (public:) mode in my "Skeleton.h" (class name)

#pragma once
#include <SFML/Graphics.hpp>
#ifndef SKELETON_H
#define SKELETON_H

class Skeleton


{
public:
    Skeleton();  // Constructor

        void Initialize();
        bool Load(const std::string& path); // Load textures or other resources
        void Update();             // Update position, animations, etc.
        void Draw();
private:
    sf::Texture texture;
    sf::Sprite sprite;
};
#endif
and it made it seem as if the problem was solved in my main.cpp

here:

int main()
{
    sf::RenderWindow window(sf::VideoMode({ 600, 600 }), "CODE SFML");

    window.setFramerateLimit({ 65 });
    sf::Clock dtClock, fpsTimer;




    std::vector<RectangleShape> bullets;

    float bulletSpeed = 1.8f;
    // now its time (deconstruct movement bullet


    sf::Texture skeletonTexture;
    sf::Sprite skeletonSprite(skeletonTexture);


    Skeleton skeleton;
    skeleton.Initialize();

    if(!skeleton.Load("assets/player/Texture/BODY_skeleton.png")) {

        skeletonSprite.setTextureRect(IntRect({ 2 * 64, 2 * 64 }, { 64, 64 }));
        skeletonSprite.setPosition(Vector2f({ 35,25 }));
        skeletonSprite.setScale(Vector2f({ 1,1 }));
        return -1; // Exit if texture fails to load
    }
However, when I run the code in the compiler, it gives me LNK2019 issues where there's an unresolved external symbol. I think it's somehow linked to the default constructors that SFML deleted for the sf::Sprite but I would like to hear your guys thoughts on it. Please, if possible, provide me the answer with an example.

my Skeleton.cpp

#include "Skeleton.h"
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;

void Skeleton::Initialize()
{
}

bool Skeleton::Load(const std::string& path)
{
    if (!texture.loadFromFile("assets / player / Texture / BODY_skeleton.png", false, sf::IntRect({2 * 64, 2 * 64}, {64, 64})))
    {
        std::cerr << "Error: Could not load texture!" << std::endl;

    sprite.setTexture(texture);
    return true;
    }
}

void Skeleton::Update()
{
}

void Skeleton::Draw()
{
}
« Last Edit: January 17, 2025, 08:18:14 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11084
    • View Profile
    • development blog
    • Email
Re: Problem with initialization of my variable to its class
« Reply #1 on: January 17, 2025, 10:23:26 am »
Sprite doesn't have a default constructor in SFML 3, as such you need to initialize it during the construction of your Skeleton class instance.

Side note: Using an Initialize() function is an anti-pattern, as your constructor should be initializing your class instance and not require a separate function.

The following constructor should fix the issue:
Skeleton::Skeleton() : sprite(texture)
{
}
 
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

alaninho_08

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with initialization of my variable to its class
« Reply #2 on: January 17, 2025, 05:19:28 pm »
Hello,
thank you for your response. It looks like I don't have a problem with my sf::sprite,but as I run my code, I only get a white square, maybe "the white square problem", but I don't see no problem with my code.

main.cpp:
Skeleton skeleton;
Orc orc;
sf::Texture texture;   
sf::Texture texture2;
sf::Sprite sprite(texture);
sf::Sprite sprite2(texture2);


if (skeleton.Load("assets/player/Texture/BODY_skeleton.png")) {
    sprite.setTexture(texture);
    sprite.setPosition(Vector2f({ 35, 25 }));
    sprite.setScale(Vector2f({ 1, 1 }));
    cout << "BODY_skeleton.png loaded successfully.\n";
}
else {
    cout << "Failed to load BODY_skeleton.png.\n";
}


if (orc.Load("assets/player/Texture/Orc.png")) {
    sprite2.setTexture(texture2);
    sprite2.setScale(Vector2f({ 2, 2 }));
    sprite2.setPosition(Vector2f({ 500, 425 }));
    cout << "Orc.png loaded successfully.\n";
}
else {
    cout << "Failed to load Orc.png.\n";
}

skeleton class: Skeleton skeleton;
Orc orc;
sf::Texture texture;   
sf::Texture texture2;
sf::Sprite sprite(texture);
sf::Sprite sprite2(texture2);


if (skeleton.Load("assets/player/Texture/BODY_skeleton.png")) {
    sprite.setTexture(texture);
    sprite.setPosition(Vector2f({ 35, 25 }));
    sprite.setScale(Vector2f({ 1, 1 }));
    cout << "BODY_skeleton.png loaded successfully.\n";
}
else {
    cout << "Failed to load BODY_skeleton.png.\n";
}


if (orc.Load("assets/player/Texture/Orc.png")) {
    sprite2.setTexture(texture2);
    sprite2.setScale(Vector2f({ 2, 2 }));
    sprite2.setPosition(Vector2f({ 500, 425 }));
    cout << "Orc.png loaded successfully.\n";
}
else {
    cout << "Failed to load Orc.png.\n";
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11084
    • View Profile
    • development blog
    • Email
Re: Problem with initialization of my variable to its class
« Reply #3 on: January 18, 2025, 02:26:57 pm »
Please use [code=cpp][/code] tags when posting code.

How does the texture get from skeleton.Load into the texture you pass to setTexture?
Same for the orc?
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything