C++ ';尺寸';:找不到标识符
我收到以下错误消息: 错误C3861:“dim”:找不到标识符 以下是我的简历:C++ ';尺寸';:找不到标识符,c++,identifier,C++,Identifier,我收到以下错误消息: 错误C3861:“dim”:找不到标识符 以下是我的简历: #include "stdafx.h" #include "HSMBTPrintX.h" #include "HSMBTPrintXCtrl.h" #include "HSMBTPrintXPropPage.h" #ifdef _DEBUG #define new DEBUG_NEW #endif 以下是我的功能: #define MSS_PORTS_BASE _T("Software\\Microsoft\
#include "stdafx.h"
#include "HSMBTPrintX.h"
#include "HSMBTPrintXCtrl.h"
#include "HSMBTPrintXPropPage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
以下是我的功能:
#define MSS_PORTS_BASE _T("Software\\Microsoft\\Bluetooth\\Serial\\Ports")
bool FindBluetoothPort(TCHAR name[16]) {
HKEY hKey, hRoot;
TCHAR szPort[20] = _T(""), szPortString[20];
DWORD len, dwIndex=0;
bool bFound=false;
INT i = 0, rc;
DWORD dwNSize;
DWORD dwCSize;
TCHAR szClass[256];
TCHAR szName[MAX_PATH];
FILETIME ft;
hRoot = HKEY_LOCAL_MACHINE;
if (RegOpenKeyEx (hRoot, MSS_PORTS_BASE, 0, 0, &hKey) != ERROR_SUCCESS) {
rc = GetLastError();
return 0;
}
dwNSize = dim(szName); <---- ~~ !! HERE IS THE LINE THAT ERRORS
dwCSize = dim(szClass); <---- HERE IS THE LINE THAT ERRORS !!
rc = RegEnumKeyEx (hKey, i, szName, &dwNSize, NULL, szClass, &dwCSize, &ft);
while (rc == ERROR_SUCCESS)
{
// how many children
TCHAR szCurrentKey[MAX_PATH];
wcscpy(szCurrentKey, MSS_PORTS_BASE);
wcscat(szCurrentKey, TEXT("\\"));
wcscat(szCurrentKey, szName);
wcscat(szCurrentKey, TEXT("\\"));
len = sizeof(szPort);
if(RegGetValue(hRoot, szCurrentKey, _T("Port"), NULL, (LPBYTE)szPort, &len)) {
wsprintf(szPortString, _T("%s:"), szPort);
bFound = true;
break;
}
dwNSize = dim(szName);
rc = RegEnumKeyEx(hKey, ++i, szName, &dwNSize, NULL, NULL, 0, &ft);
}
if(bFound)
_tcscpy(name, szPortString);
return bFound;
}
template<typename T, size_t N>
size_t dim(const T (& array)[N])
{
return N;
}
为什么这是一个错误?看起来您需要
sizeof
:
dwNSize = sizeof(szName);
dwCSize = sizeof(szClass);
sizeof
返回对象/变量的字节数。但是,我只是查看了API RegEnumKeyEx的文档,它需要多少字符。因此,我认为它实际上应该除以一个TCHAR的大小(这将是1或2,取决于您是否为Unicode构建)
您需要
sizeof
如果您最初学习了
dim
,那么这可能是一个真正在幕后调用sizeof
的宏。我以前使用过以下宏:
#define DIM(x) (sizeof(x)/sizeof((x)[0]))
它不是由任何标准包括提供的,您必须自己定义它
您还可以使用模板功能制作更现代的版本:
#define MSS_PORTS_BASE _T("Software\\Microsoft\\Bluetooth\\Serial\\Ports")
bool FindBluetoothPort(TCHAR name[16]) {
HKEY hKey, hRoot;
TCHAR szPort[20] = _T(""), szPortString[20];
DWORD len, dwIndex=0;
bool bFound=false;
INT i = 0, rc;
DWORD dwNSize;
DWORD dwCSize;
TCHAR szClass[256];
TCHAR szName[MAX_PATH];
FILETIME ft;
hRoot = HKEY_LOCAL_MACHINE;
if (RegOpenKeyEx (hRoot, MSS_PORTS_BASE, 0, 0, &hKey) != ERROR_SUCCESS) {
rc = GetLastError();
return 0;
}
dwNSize = dim(szName); <---- ~~ !! HERE IS THE LINE THAT ERRORS
dwCSize = dim(szClass); <---- HERE IS THE LINE THAT ERRORS !!
rc = RegEnumKeyEx (hKey, i, szName, &dwNSize, NULL, szClass, &dwCSize, &ft);
while (rc == ERROR_SUCCESS)
{
// how many children
TCHAR szCurrentKey[MAX_PATH];
wcscpy(szCurrentKey, MSS_PORTS_BASE);
wcscat(szCurrentKey, TEXT("\\"));
wcscat(szCurrentKey, szName);
wcscat(szCurrentKey, TEXT("\\"));
len = sizeof(szPort);
if(RegGetValue(hRoot, szCurrentKey, _T("Port"), NULL, (LPBYTE)szPort, &len)) {
wsprintf(szPortString, _T("%s:"), szPort);
bFound = true;
break;
}
dwNSize = dim(szName);
rc = RegEnumKeyEx(hKey, ++i, szName, &dwNSize, NULL, NULL, 0, &ft);
}
if(bFound)
_tcscpy(name, szPortString);
return bFound;
}
template<typename T, size_t N>
size_t dim(const T (& array)[N])
{
return N;
}
模板
大小尺寸(常量和数组)[N])
{
返回N;
}
<代码>你用什么教程来学习C++?C++不使用“代码> Dime”这是我从其他地方获得的代码。我们(我们的公司)通过蓝牙从无线扫描仪上获得了这一功能。。并告诉我们需要实现activeX。我是幸运的一个人,我能弄明白这一点,显然我也试着修理某些部件:)显然,另一家公司的人需要更换灯泡。这是有道理的。。。我只是更加注意变量名,而不是让自己迷失在困惑中。非常感谢。这是有道理的。。。我只是更加注意变量名,而不是让自己迷失在困惑中。非常感谢。看到这样的代码片段,我真的很想学习C++。做VB.net已经三年了,到处都做了一点java,但没有什么先进的东西。@Adam,这算不上是模范代码,我对你的评论感到惊讶。按照今天的标准,宏是非常原始的。@Adam我添加了模板版本,至少对我来说有点有趣。