尝试编译程序时出现此错误:
Error 1 error LNK2001: unresolved external symbol "public: static class sf::Texture TextureManager::texture" (?texture@TextureManager@@2VTexture@sf@@A)
这是我的代码:
main.cpp中:
int main()
{
TextureManager::Initialize();
}
TextureManager.h:
#include <SFML\Graphics.hpp>
using namespace sf;
class TextureManager
{
public:
static Texture texture;
public:
static void Initialize();
};
TextureManager.cpp:
#include <SFML\Graphics.hpp>
#include <iostream>
#include "TextureManager.h"
using namespace sf;
void TextureManager::Initialize()
{
if(!texture.loadFromFile("Textures\\Blocks\\Texture.png"))
{
std::cout << "Error!";
}
else
{
std::cout << "Sucess!";
}
}
我已尝试搜索任何解决方案(包括此网站),但未找到任何解决方案。
答案 0 :(得分:6)
如果在C ++中有静态成员,则应在.cpp中定义它:
static Texture Texture::texture;
这是因为静态成员必须只在一个翻译单元中定义,以免违反One-Definition Rule。
您可以在TextureManager.cpp的顶部执行此操作:
#include <SFML\Graphics.hpp>
#include <iostream>
#include "TextureManager.h"
using namespace sf;
static Texture Texture::texture; // <-
void TextureManager::Initialize()
{
}