“默认参数”:不能从“const char[1]”转换为“const wchar_t *”

时间:2022-12-04 05:53:08

I'm trying to compile some old game SRC I found on the net here's the code

我正在尝试编译一些旧的游戏SRC,我在网上找到了这是代码。

bool LoadFromINI(std::wstring const& strINIFileName = _T("./Local.ini"), char const* szDefaultLocale = "");
bool LoadFromINB(std::wstring const& strINBFileName, wchar_t const* szDefaultLocale = _T(""));


C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(60): error C2440: 'default argument' : cannot convert from 'const char [1]' to 'const wchar_t *'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(60): error C2548: 'LOCAL_MGR::CLocal::LoadFromINB' : missing default parameter for parameter 2
1>C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(59): error C2440: 'default argument' : cannot convert from 'const char [12]' to 'const std::wstring &'
1>          Reason: cannot convert from 'const char [12]' to 'const std::wstring'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(103): fatal error C1903: unable to recover from previous error(s); stopping compilation

One more error: Code:

一个错误:代码:

      _tcscpy_s(m_kDBName,30, (wchar_t const*)in_strDBName);

Output:

输出:

'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot convert parameter 3 from 'const wchar_t *' to 'const char *' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

“errno_t strcpy_s(char *,rsize_t,const char *)”:不能将参数3从“const wchar_t *”转换为“const char *”1>类型指向的是不相关的;转换需要重新解释、c风格的转换或功能样式的转换。

2 个解决方案

#1


2  

Change _T("blah") to L"blah".

改变_T(“废话”)到L“废话”。

_T is a macro that does nothing or adds an L.

_T是一个什么都不做或添加一个L的宏。

Alternatively, compile project with the wchar option for _T and TCHAR.

或者,使用wchar选项(_T和TCHAR)编译项目。

#2


0  

The _T("str") expands to L"str" only if your project is compiled with the UNICODE preprocessor symbol defined. In your case, it seems it isn't, so _T() does nothing. Change the function declarations to

只有当您的项目被定义为UNICODE预处理器符号时,_T(“str”)才会扩展到L“str”。在您的情况中,似乎没有,所以_T()什么都不做。将函数声明更改为。

bool LoadFromINI(std::wstring const& strINIFileName = L"./Local.ini", wchar_t const* szDefaultLocale = "");
bool LoadFromINB(std::wstring const& strINBFileName, wchar_t const* szDefaultLocale = L"");

or if you really, really must support the _T() and TCHAR stuff, change them to

或者如果你真的,真的必须支持_T()和TCHAR的东西,改变它们。

bool LoadFromINI(std::basic_string<TCHAR> const& strINIFileName = _T("./Local.ini"), TCHAR const* szDefaultLocale = "");
bool LoadFromINB(std::basic_string<TCHAR> const& strINBFileName, TCHAR const* szDefaultLocale = _T(""));

Now the first argument will be either std::string or std::wstring depending on whether UNICODE is defined.

现在第一个参数是std::string或std::wstring,取决于是否定义了UNICODE。

#1


2  

Change _T("blah") to L"blah".

改变_T(“废话”)到L“废话”。

_T is a macro that does nothing or adds an L.

_T是一个什么都不做或添加一个L的宏。

Alternatively, compile project with the wchar option for _T and TCHAR.

或者,使用wchar选项(_T和TCHAR)编译项目。

#2


0  

The _T("str") expands to L"str" only if your project is compiled with the UNICODE preprocessor symbol defined. In your case, it seems it isn't, so _T() does nothing. Change the function declarations to

只有当您的项目被定义为UNICODE预处理器符号时,_T(“str”)才会扩展到L“str”。在您的情况中,似乎没有,所以_T()什么都不做。将函数声明更改为。

bool LoadFromINI(std::wstring const& strINIFileName = L"./Local.ini", wchar_t const* szDefaultLocale = "");
bool LoadFromINB(std::wstring const& strINBFileName, wchar_t const* szDefaultLocale = L"");

or if you really, really must support the _T() and TCHAR stuff, change them to

或者如果你真的,真的必须支持_T()和TCHAR的东西,改变它们。

bool LoadFromINI(std::basic_string<TCHAR> const& strINIFileName = _T("./Local.ini"), TCHAR const* szDefaultLocale = "");
bool LoadFromINB(std::basic_string<TCHAR> const& strINBFileName, TCHAR const* szDefaultLocale = _T(""));

Now the first argument will be either std::string or std::wstring depending on whether UNICODE is defined.

现在第一个参数是std::string或std::wstring,取决于是否定义了UNICODE。