windows 错误 C2446:==:没有从 const char * 到 TCHAR * 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4201893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:37:10  来源:igfitidea点击:

error C2446: == : no conversion from const char * to TCHAR *

c++windowstchar

提问by Simsons

I have a TCHAR define below:

我有一个 TCHAR 定义如下:

 TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

and I want to comapare as below:

我想comapare如下:

if(szProcessName == "NDSClient.exe")
{
} 

But then I am getting the errors:

但是后来我收到了错误:

error C2446: == : no conversion from const char * to TCHAR *
error C2440: '==' : cannot convert from 'const char [14]' to 'TCHAR [260]'

错误 C2446:==:无法从 const char * 转换为 TCHAR *
错误 C2440:“==”:无法从“const char [14]”转换为“TCHAR [260]”

回答by Naveen

"NDSClient.exe"is a const char*string on windows. If you want it to become a const TCHAR*then you need to use the TEXTmacro. Also, you can not compare strings using ==use a equivalent TCHARfunction such as _tcscmp.

"NDSClient.exe"const char*windows 上的字符串。如果你想让它变成 aconst TCHAR*那么你需要使用TEXT宏。此外,您不能==使用等效TCHAR函数(例如_tcscmp.

回答by Mihran Hovsepyan

Also you can use. L"some string"to make TCHAR*. But I suggest you to use std::wstring(analog of std::stringand as std::stringneeds #include <string>) instead of TCHAR*.

你也可以使用。L"some string"使 TCHAR*。但我建议您使用std::wstring(模拟std::string和根据std::string需要#include <string>)而不是 TCHAR*。

example:

例子:

#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
 wstring s = TEXT("HELLO");
 wstring ss = L"HELLO";
 if(s == ss)
  cout << "hello" << endl;
 return 0;
}