在头文件的数组中定义资源 ID
Defining Resource ID's in an array in a header file
我试图在头文件中以矢量格式定义RID的连续范围:
#include<vector>
#include<stdlib.h>
#include<iostream>
#define vector<int> IDM_POPUP_LAST (5);
for(i = 0; i < IDM_POPUP_LAST.size(); i++)
IDM_POPUP_LAST [i] = i + 90;
这里少了什么吗。我有一组错误:
您正在尝试静态初始化数据,并且希望它位于vector
的对象中(如果我理解正确的话)。不幸的是,这在C++中是不可能的。然而,你还可以探索其他选择:
1) 使用静态数组,类似于以下内容:
int IDM_POPUP_LAST[] = {1, 2, 3, 4, 5};
2) 有一个向量在main()
的早期初始化,或者由伪类的构造函数初始化,如下所示:
vector<int> IDM_POPUP_LAST(5);
struct DummyInitializer
{
DummyInitializer()
{
// code to initialize the vector IDM_POPUP_LAST
}
} global_var_so_the_constructor_is_called_before_main;
您的变量声明是错误的。变量通常使用以下语法声明:
std::vector<int> IDM_POPUP_LAST (5);
除此之外,for
不能简单地放在函数之外。
话虽如此,这可能是一个全局变量。绕过这一点的一种方法是使它成为类的静态成员,并使用一个函数对其进行初始化。当你决定需要其他类型的id时,你甚至可以在这里添加它们,并根据需要将初始化它们添加到函数中:
//resource.h
struct ResourceIDs {
static std::vector<int> menus;
static void init();
//Let's add in three cursors
static std::vector<int> cursors;
};
//NOTE - these are all better off in a .cpp
#include "resources.h" //if in the cpp
std::vector<int> ResourceIDs::menus (5); //define the menus member
std::vector<int> ResourceIDs::cursors (3); //define the cursors member
void ResourceIDs::init() {
for (int i = 0; i < menus.size(); ++i) //note the type mismatch since vector
menus[i] = i + 90; //uses size_t, which is unsigned
//let's do cursors starting at 150
for (int i = 0; i < cursors.size(); ++i)
cursors[i] = i + 150;
}
现在你只需要确保初始化它们,然后你可以在任何需要的地方使用它们:
#include <windows.h>
#include "resource.h"
int main() {
ResourceIDs::init();
//create window, message loop, yada yada
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND:
//check the menu ids using ResourceIDs::menus[x]
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
}
这里与代码#定义id的样子的唯一区别是在main
开始时调用ResourceIDs::init()
、需要ResourceIDs::
以及数组语法。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 具有瞬态资源的RAII类
- #定义c-预处理器常量..我做错了什么
- C++链接时间资源"allocation"而不定义
- 带有资源句柄的TensorFlow自定义C++操作
- 如何创建一个随机访问式的Rreference以访问.RC文件中定义的资源
- TensorFlow中的自定义资源
- 在 MFC 应用中定义和读取用户定义的资源
- 逐行读取用户定义的文本资源文件
- 如何使用 Windows API 自定义对话框模板的资源
- 我在哪里定义代码来释放在C Python模块init函数中分配的资源
- 在头文件的数组中定义资源 ID
- RCDATA 和用户自定义资源有什么区别?
- CppUtest在测试之间共享资源,未定义引用
- 如何在VC++静态库中加载自定义二进制资源作为dll的一部分
- visual C++自定义二进制资源文件
- 我可以通过命令行将预处理器定义传递给资源编译器吗?
- 如何在没有资源编译器的情况下将自定义窗口的资源添加到可执行文件中?
- 如何获取控件自定义资源数据
- Qt- 从资源添加自定义字体