C++ Thor 库 - 使用资源加载器时出现问题 class(“”未命名类型)
C++ Thor library - problem with using resource loader class ( ' ' does not name a type)
我最近一直在练习管理多个对象并使用 SFML 库在 C++ 中绘制它们。我希望我的纹理和未来的资源更加可重用,所以我决定使用非常适合我需要的 Thor 库。
所以我已经根据您在 this tutorial 中找到的内容编写了前几行代码,编译器总是说:
main.cpp|12|error: 'textures_holder' does not name a type
这一行报错:
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png"));
我正在使用 Code::Blocks IDE 与 MinGW 编译器和 SFML 2.5.0。
这是我的 main.cpp 和包含外部对象的头文件:
//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"
using namespace std;
//Adding textures to the texture library
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png"));
//Rest of code...
Dirt_Block.h(只有上半部分):
#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H
#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code
我想知道是什么导致了这个错误,也许可以帮助其他可能遇到类似令人沮丧的问题的人。感谢您的帮助。
编辑:
正如评论中所建议的,我在 Dirt_Block.h 中声明了一些 extern int 变量,所以现在看起来像这样:
//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...
然后在 main.cpp 中给他们一些价值:
//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png"));
//...
但是编译报错:
main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type
在没有所有无关代码的情况下,查看您的问题的注意力要少得多!
C++ 程序不会从文件的顶部开始,运行 代码不会从底部开始。它们从 main() 开始,控制流从那里开始,一件事触发另一件事。
(注意:这没有考虑 global constructor ordering,它确实按照声明的顺序进行——但您无法保证 "different files" 的顺序声明可能 运行 英寸)
要点是,您不能只是在文件中间进行随机函数或方法调用。那就是你放置声明的地方。您必须在函数或方法内部才能进行调用,例如
int main() {
textures_holder.acquire(
"Dirt",
thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png")
);
...
}
我最近一直在练习管理多个对象并使用 SFML 库在 C++ 中绘制它们。我希望我的纹理和未来的资源更加可重用,所以我决定使用非常适合我需要的 Thor 库。
所以我已经根据您在 this tutorial 中找到的内容编写了前几行代码,编译器总是说:
main.cpp|12|error: 'textures_holder' does not name a type
这一行报错:
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png"));
我正在使用 Code::Blocks IDE 与 MinGW 编译器和 SFML 2.5.0。
这是我的 main.cpp 和包含外部对象的头文件:
//...
#include <Thor/Resources.hpp>
#include "Dirt_Block.h"
using namespace std;
//Adding textures to the texture library
//THIS LINE GIVES AN ERROR
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png"));
//Rest of code...
Dirt_Block.h(只有上半部分):
#ifndef DIRT_BLOCK_H
#define DIRT_BLOCK_H
#include <SFML\Graphics.hpp>
#include <vector>
#include <Thor/Resources.hpp>
#include <Thor/Resources/SfmlLoaders.hpp>
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
//Rest of the code
我想知道是什么导致了这个错误,也许可以帮助其他可能遇到类似令人沮丧的问题的人。感谢您的帮助。
编辑:
正如评论中所建议的,我在 Dirt_Block.h 中声明了一些 extern int 变量,所以现在看起来像这样:
//...
extern int test_int_up;
extern sf::Vector2u screenRes;
extern thor::ResourceHolder<sf::Texture, std::string> textures_holder;
extern int test_int_d;
//...
然后在 main.cpp 中给他们一些价值:
//...
test_int_up = 55;
test_int_d = 55;
//Adding textures to the texture library
textures_holder.acquire("Dirt", thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png"));
//...
但是编译报错:
main.cpp|9|error: 'test_int_up' does not name a type
main.cpp|10|error: 'test_int_d' does not name a type
main.cpp|12|error: 'textures_holder' does not name a type
在没有所有无关代码的情况下,查看您的问题的注意力要少得多!
C++ 程序不会从文件的顶部开始,运行 代码不会从底部开始。它们从 main() 开始,控制流从那里开始,一件事触发另一件事。
(注意:这没有考虑 global constructor ordering,它确实按照声明的顺序进行——但您无法保证 "different files" 的顺序声明可能 运行 英寸)
要点是,您不能只是在文件中间进行随机函数或方法调用。那就是你放置声明的地方。您必须在函数或方法内部才能进行调用,例如
int main() {
textures_holder.acquire(
"Dirt",
thor::Resources::fromFile<sf::Texture>("Textures\dirt_block.png")
);
...
}