如何将 LPTSTR 转换为 LPCTSTR&
How to convert LPTSTR to LPCTSTR&
函数参数为 LPCTSTR&
。
我必须通过LPTSTR
变量为LPCTSTR&
。
如何将LPTSTR
转换为LPCTSTR&
。
预先感谢。
从我旧的C 体验中,您试图通过参考将指针传递给const字符串。编译器认为您将更改指针值。因此,您有2个选项
- 使参数const so编译器可以接受lpttr。
- 或创建一个LPCTSTR指针(可以更改的LVALUE)并传递。
我必须尝试在以下代码段中解释它。我使用VS 2017 Windows 7 SDK 10
void Foo(LPCTSTR &str)
{
std::wcout << str;
str = _T("World");
}
void FooConst(LPCTSTR const &str)
{
std::wcout << str;
//str = _T("World"); will give error
}
int main()
{
LPTSTR str = new TCHAR[10];
LPCTSTR str1 = str;
lstrcpy(str, _T("Hello"));
// Foo(str);// Error E0434 a reference of type "LPCTSTR &" (not const - qualified) cannot be initialized with a value of type "LPTSTR" HelloCpp2017
// Foo(static_cast<LPCTSTR>(str));// Error(active) E0461 initial value of reference to non - const must be an lvalue HelloCpp2017 d : jfksamplescppHelloCpp2017HelloCpp2017HelloCpp2017.cpp 19
// Tell compiler you will not change the passed pointer
FooConst(str);
// Or provide a lvalue pointer that can be changed
Foo(str1);
std::wcout << str1;
return 0;
}
相关文章:
- 如何在Windows上将Qt QString转换为LPCTSTR
- 将 LPSTR 转换为 LPCTSTR
- 无法从'const char *'转换为'LPCTSTR'
- 如何在 C++(MFC) 中将 char* 转换为 LPCTSTR
- 将浮点数转换为 LPCTSTR 转换
- 将WCHAR转换为LPCTSTR
- 将cryptopp :: Integer转换为lpctstr
- 仅分配第一个字符,将 LPCTSTR 转换为字符*
- 如何将 LPTSTR 转换为 LPCTSTR&
- 如何将char []转换为lpctstr
- CString 到 LPCTSTR 的转换会中断 sql 查询
- 无法将参数 1 从"const char [5]"转换为"LPCTSTR"
- CString 到 LPCTSTR 的转换
- LPCTSTR 到 LPCSTR 的转换
- 如何将字符数组转换为 LPCTSTR
- 错误 C2664:'CComboBox::InsertString':无法将参数 2 从 'const char [4]' 转换为 'LPCTSTR'
- 使用 sstream 将 int 转换为 LPCTSTR 时出现奇怪的结果
- 无法在 MFC / C++ 项目中将参数 1 从'const wchar_t *'转换为'LPCTSTR'
- win32 API createFile 函数:转换为 LPCTSTR
- 字符串转换为LPCTSTR