将MFC CMap用于CString int对 [英] using MFC CMap for CString int pairs
问题描述
我当前正在处理一个DLL,该DLL需要在值的友好名称和值本身之间来回转换.由于该代码在整个代码库中的许多地方都使用过,因此我想尝试使其保持简单并在单个函数或对象中使用,因此我只需要声明一次即可.
I'm currently working on a DLL that needs to convert back and forth between the friendly name for a value and the value itself. As this code is used in many places throughout the codebase, I want to try and keep it simple and in a single function or object so I only have to declare them once.
从我的阅读看来,CMap
是完成这项工作的工具,但是我似乎无法发现可以无错误编译的模板参数的任何组合.
From my reading it looks like CMap
is the tool for the job, but I can't seem to discover any combination of template arguments that compiles without errors.
我的值是CString
和int
.我尝试了以下定义:
My values are CString
and int
. I tried the following definition:
CMap<int, int, CString, CString> encodermap;
可以编译,但是当我尝试添加一个值时:
which compiles, but when I try to add a value:
encodermap["Encoder 1"] = 0;
我收到以下编译器错误:
I get the following compiler errors:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2501: 'encodermap' : missing storage-class or type specifiers
error C2040: 'encodermap' : 'int []' differs in levels of indirection from 'CMap<KEY,ARG_KEY,VALUE,ARG_VALUE>'
我尝试将CMap
更改为此:
CMap<CString, CString, int, int> encodermap;
但是我遇到了同样的四个错误.
but I get the same four errors.
我确定我一定会丢失一些东西,但是我对什么感到茫然.
I'm sure I must be missing something but I'm at a loss as to what.
由于要使用此SDK,因此我需要VS2003
Because of the SDK being used for this work I'm require VS2003
推荐答案
问题
我认为您已经将键类型和谷值类型颠倒了.
I think you've inverted the key type and the valye type.
您的原始声明将int
定义为要使用operator[]
搜索的关键字.因此encodermap[0] = "Encoder 1";
将起作用.
Your original declaration defines int
as being the key to be searched for with operator[]
. So encodermap[0] = "Encoder 1";
would work.
但是当您的编译器看到encodermap["Encoder 1"] = 0;
时,他试图找到一个使用char *(或char *可以转换为的东西)并返回int的operator[]
.最后一条错误消息告诉您,他无法为您的地图找到这样的运算符.
But when your compiler sees encodermap["Encoder 1"] = 0;
, he tries to find an operator[]
which takes char* (or something to which char * can be converted to) and returns an int. The last error message tells you that he couldn't find such an operator for your map.
使用MSVC 2015,错误消息更加简洁: C2679 .
With MSVC 2015, the error message is more concise: C2679.
解决方案
您应使用CString
定义 CMap
键和int
值.要知道的诀窍是,对于CString
密钥,ARG_KEY应该为LPCWSTR
.因此正确的定义是:
You should define your CMap
with a CString
key and an int
value. The trick to know, is that for a CString
KEY, the the ARG_KEY should be LPCWSTR
. So the right definition would be:
CMap<CString, LPCWSTR, int, int> encodermap;
这允许使用CString
作为地图 operator[]
.
This permits to use CString
as key in the map's operator[]
.
现在,如果您在Windows上使用MFC,则可能会使用UNICODE和宽字符(因此是LPCWSTR而不是LPCSTR).调用运算符时,您必须使用CString或宽文字:
Now if you use MFC on windows, you probably use UNICODE and wide chars (therefore the LPCWSTR instead of LPCSTR). When calling the operator you then have either to use a CString or a wide literal:
encodermap[L"Encoder 1"] = 0;
encodermap[CString("Encoder 2")] = 1;
这篇关于将MFC CMap用于CString int对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!