T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//
代码见上,VC6编译的时候报
error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2975: 'THandle' : invalid template argument for 'CloseFunc', constant expression expected see declaration of 'THandle'
.......
WHY?
13 个解决方案
#1
在线等。立即给分。
#2
error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
传入的函数指针不匹配, 如果不是错误的话,可以强制转换
return (*CloseFunc)(h);
h不是类的成员变量
传入的函数指针不匹配, 如果不是错误的话,可以强制转换
return (*CloseFunc)(h);
h不是类的成员变量
#3
我在VC6 下编译没问题啊!
#4
我的QQ 361017175。在线请教。
#5
我是觉得是能够强制转换的。因为这个代码是从别人工程中拷贝的,别人能编译过。
#6
把你的变量定义贴出来看看,我这样可以编译运行
#include <windows.h>
#pragma comment (lib,"advapi32")
#pragma comment (lib,"user32")
template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//
void main()
{
CHandle hd;
hd.Close();
CHKey ke;
ke.Close();
CHFile fe;
fe.Close();
CHFind fd;
fd.Close();
CHChange ce;
ce.Close();
CHModule me;
me.Close();
CHWnd wd;
wd.Close();
}
#include <windows.h>
#pragma comment (lib,"advapi32")
#pragma comment (lib,"user32")
template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//
void main()
{
CHandle hd;
hd.Close();
CHKey ke;
ke.Close();
CHFile fe;
fe.Close();
CHFind fd;
fd.Close();
CHChange ce;
ce.Close();
CHModule me;
me.Close();
CHWnd wd;
wd.Close();
}
#7
我还没定义变量。单单你上面那些我就编译不过。你的VC有安装SP3吗?
#8
和VC的设置有关吗?
#9
应该与设置无关吧,我在命令行及IDE下都能通过的
我的VC已安装了sp5
装个sp5试试(microsoft网站上已有sp6了)
我的VC已安装了sp5
装个sp5试试(microsoft网站上已有sp6了)
#10
我也是安装了SP5。
这个错误在MSDN上被视为VS的一个BUG。但是是说安装SP3就可以搞定,我都安装SP5了。
这个错误在MSDN上被视为VS的一个BUG。但是是说安装SP3就可以搞定,我都安装SP5了。
#11
你在命令行下试试(这样可排除设置问题),不行重打sp5或下个SP6(你的SP5没打好或不全?)
#12
好了,是SP5的问题。
#13
MSDN上面已经说过这个错误是VS的一个BUG,打SP3就可以。我打了SP5还是同样报错。然后下了个英文版的SP6,编译通过。估计是我下载的SP5存在问题。
谢谢keiy 。已给分。
谢谢keiy 。已给分。
#1
在线等。立即给分。
#2
error C2440: 'specialization' : cannot convert from 'int (__stdcall *)(void *)' to ' (__stdcall *)()'
传入的函数指针不匹配, 如果不是错误的话,可以强制转换
return (*CloseFunc)(h);
h不是类的成员变量
传入的函数指针不匹配, 如果不是错误的话,可以强制转换
return (*CloseFunc)(h);
h不是类的成员变量
#3
我在VC6 下编译没问题啊!
#4
我的QQ 361017175。在线请教。
#5
我是觉得是能够强制转换的。因为这个代码是从别人工程中拷贝的,别人能编译过。
#6
把你的变量定义贴出来看看,我这样可以编译运行
#include <windows.h>
#pragma comment (lib,"advapi32")
#pragma comment (lib,"user32")
template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//
void main()
{
CHandle hd;
hd.Close();
CHKey ke;
ke.Close();
CHFile fe;
fe.Close();
CHFind fd;
fd.Close();
CHChange ce;
ce.Close();
CHModule me;
me.Close();
CHWnd wd;
wd.Close();
}
#include <windows.h>
#pragma comment (lib,"advapi32")
#pragma comment (lib,"user32")
template<class T, class CloseT, CloseT (_stdcall * CloseFunc)(T)> class THandle {
T m_h;
public:
THandle() {
m_h = T(-1); // Always illegal
}
THandle(T h) { m_h = h; }
~THandle() {
if (IsValid()) Close(); // Ensure we do not double-close
}
THandle<T, CloseT, CloseFunc>& operator=(T h) {
m_h = h;
return *this;
}
operator T() { return m_h; }
T *operator &() { return &m_h; }
CloseT Close() {
T h = m_h;
m_h = T(-1);
return (*CloseFunc)(h); // Return the result of whatever type
};
BOOL IsValid() { return m_h != T(-1) && m_h != T(0); }
};
//
// Now define common handles as safe classes.
//
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHandle;
typedef class THandle<HKEY, LONG, &RegCloseKey> CHKey;
typedef class THandle<HANDLE, BOOL, &CloseHandle> CHFile;
typedef class THandle<HANDLE, BOOL, &FindClose> CHFind;
typedef class THandle<HANDLE, BOOL, &FindCloseChangeNotification> CHChange;
typedef class THandle<HMODULE, BOOL, &FreeLibrary> CHModule;
typedef class THandle<HWND, BOOL, &DestroyWindow> CHWnd;
//
void main()
{
CHandle hd;
hd.Close();
CHKey ke;
ke.Close();
CHFile fe;
fe.Close();
CHFind fd;
fd.Close();
CHChange ce;
ce.Close();
CHModule me;
me.Close();
CHWnd wd;
wd.Close();
}
#7
我还没定义变量。单单你上面那些我就编译不过。你的VC有安装SP3吗?
#8
和VC的设置有关吗?
#9
应该与设置无关吧,我在命令行及IDE下都能通过的
我的VC已安装了sp5
装个sp5试试(microsoft网站上已有sp6了)
我的VC已安装了sp5
装个sp5试试(microsoft网站上已有sp6了)
#10
我也是安装了SP5。
这个错误在MSDN上被视为VS的一个BUG。但是是说安装SP3就可以搞定,我都安装SP5了。
这个错误在MSDN上被视为VS的一个BUG。但是是说安装SP3就可以搞定,我都安装SP5了。
#11
你在命令行下试试(这样可排除设置问题),不行重打sp5或下个SP6(你的SP5没打好或不全?)
#12
好了,是SP5的问题。
#13
MSDN上面已经说过这个错误是VS的一个BUG,打SP3就可以。我打了SP5还是同样报错。然后下了个英文版的SP6,编译通过。估计是我下载的SP5存在问题。
谢谢keiy 。已给分。
谢谢keiy 。已给分。