定义资源的ID在头文件
问题描述:
我试着在头文件矢量格式来定义RID的一个连续范围的数组:定义资源的ID在头文件
#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);
}
}
这里从定义-ING的ID应该是什么代码#像唯一的区别是在main
开始到ResourceIDs::init()
一个电话,需要ResourceIDs::
和数组语法。
哦,那就是你的意思。您需要将这些底部3移动到您使用它们的任何函数中。 – chris 2012-07-25 00:55:54
我试图将RID传递给函数,以便可以在那里处理。那么我怎样才能将其他RID移动到正在使用的函数中。我应该在头文件中定义IDM_POPUP_LAST的RID? – crr 2012-07-25 01:01:27
当您使用'define'时,'IDM_POPUP_LAST'只是一种类型,您不能将其用作变量。 – aasa 2012-07-25 01:08:25