无法从“ const wchar_t *”转换为'_tchar *'
-
14-09-2019 - |
题
_TCHAR* strGroupName = NULL;
const _TCHAR* strTempName = NULL;
//Assign some value to strTempName
strGroupName = _tcschr(strTempName, 92) //C2440
在VS2008中编译此代码时,我会在上面的行中遇到错误。在VC6中,它可以很好地编译。
错误C2440:'=':无法从'const wchar_t *'转换为'_tchar *'
似乎是什么问题,我该如何解决?
解决方案
尝试将其施放为
strGroupName = (_TCHAR*)_tcschr(strTempName, 92);
在我看来,VS2008对类型的演员有些严格,并且在某些情况下不会自动执行。
其他提示
strGroupName = const_cast<_TCHAR*>( _tcschr(strTempName, 92));
这是因为您使用的函数的变体具有const _tchar*作为输入,并返回const _tchar*。
另一个变体是将strtempname称为_tchar*,而不是const _tchar*。在这种情况下,使用具有_tchar*参数并返回_tchar*值的变体函数。
strGroupName
也应该是指向 const
.
const _TCHAR* strGroupName = _tcschr(strTempName, 92);
无需声明它,直到呼叫初始化为止。
_tcschr
正在返回const指针。因此,回报值应该是 const _TCHAR* strGroupName = NULL;
如果不可能将strgroupname更改为const指针,则将两个指针声明为非符合指针。
不隶属于 StackOverflow