错误 C2664:'callToPrint':无法将参数 1 从 'std::wstring' 转换为'LPTSTR'
error C2664: 'callToPrint' : cannot convert parameter 1 from 'std::wstring' to 'LPTSTR'
我将一个窄字符串转换为宽字符串,如下所示:
string nameOfPrinter;
getline( cin , nameOfPrinter );
wstring WprinterName;
int number = MultiByteToWideChar( CP_UTF8 , 0 , nameOfPrinter.c_str() , nameOfPrinter.size() , &WprinterName , 0 );
// then i make a call to the function whose prototype is callToPrint( LPTSTR , LPVOID , DWORD , string )
// the calling statement is :
callToPrint( WprinterName , -----all other arguments-----,);
// But this call produces the following error error C2664: 'callToPrint' : cannot convert parameter 1 from 'std::wstring' to 'LPTSTR'
为什么会这样?请告诉我怎样才能解决这个问题
这里还需要使用。c_str()。
此外,我还使用
将打印机名称直接读入WprinterName。getline(wcin, Wprintername);
你的问题是callToPrint
基本上声明它期望一个可以修改的C字符串,即不是const
。因此使用LPTSTR
代替LPTCSTR
VC宏。它是否实际上改变了缓冲区取决于它的实现。现在,w_string.c_str()
返回一个const wchar_t*
,根据c_str()的定义,你不能改变它(即使你可以将它强制转换为wchar_t*
,在这种情况下,你的代码将被编译)。
由于callToPrint
是以这种方式声明的,因此必须为它提供一个非const C字符串。为此,您可以放弃使用wstring WprinterName
,而使用wchar_t(或者TCHAR,如果您想坚持使用VC类型)的原始数组。在MultiByteToWideChar
和callToPrint
中使用这个缓冲区,不要忘记在最后释放它…
如果您确实需要wstring
进行进一步处理,请阅读:Convert std::string to const char*或char*以获得一些额外的建议
相关文章:
- -fshort-wchar 和 std::wstring - 分段错误
- 如何将std::wstring转换为char const[]
- 将utf16宽std::wstring转换为utf8窄std::string以获得罕见字符时出现问题
- 找不到 std::wstring 中存在的wchar_t
- 将整数附加到 std::wstring 会给出错误
- 使用STD :: WSTRING或WCHAR_T登录
- 从 std::wstring 转换为 jstring
- UTF8 数据到 std::string 或 std::wstring
- 将 std::u16string 转换为 std::wstring,无需复制
- 从相同的硬编码字符串文字初始化std::string和std::wstring
- std::wstring中ASCII符号的字节表示,具有不同的语言环境
- std::wstring 和 LOGFONT 重载冲突 - 为什么?
- 为什么我的 BSTR 到 std::wstring 的转换这么慢?我的测试仪不好吗?
- 将 std::string 移植到 std::wstring,反之亦然
- 从DLL返回std::wstring是否安全
- 在std::string和std::wstring之间转换的多平台方式
- 正在将std::wstring转换为const wchar_t x[]
- std::wstring指针赋值C++
- porting std::wstring to gcc cygwin
- 结合 std::wstring 和函数