最初,我试图将我的 main.cpp 分离到我正在尝试构建的 SFML 3.0.0 游戏中每个玩家的不同类,但是当尝试将局部变量分配给我的类时,编译器给了我一个错误说“无法引用已删除的函数” 显示错误描述的图像。
所以,我尝试在“Skeleton.h”(类名)中的(public:)模式中使用包含我的类名
#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
这使得问题似乎在我的 main.cpp 中得到了解决
这里:
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
}
但是,当我在编译器中运行代码时,它会出现 LNK2019 问题,其中存在未解析的外部符号。我认为它与 SFML 为 sf::Sprite 删除的默认构造函数有某种联系,但我想听听你们对此的想法。
我的骨架.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()
{
}
尝试在 Skeleton.cpp 中添加默认构造函数的实现
Skeleton::Skeleton()
{
}