Concatenate CString and Long in VC++? - c++

I have to concatenate two CString variables and two long variables in one
CString. I found one Format function that I have used like this:
CString str = "Some Data";
str.Format("%s%d", str, 123);
But it is giving errors. Here is the error log:
\AudWinSockXCtrl.cpp(410) : error C2440: 'initializing' : cannot convert from 'const char [10]' to 'ATL::CStringT'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
Constructor for class 'ATL::CStringT' is declared 'explicit'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
.\AudWinSockXCtrl.cpp(411) : error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' :
cannot convert parameter 1 from 'const char [5]' to 'const wchar_t *'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast
.\AudWinSockXCtrl.cpp(414) : error C2664: 'void ATL::CStringT::Format(const wchar_t *,...)' :
cannot convert parameter 1 from 'const char [4]' to 'const wchar_t *'
with
[
BaseType=wchar_t,
StringTraits=StrTraitMFC
]
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or
function-style cast
Is there any function like toString() like we use in Java?

CString str = _T("Some Data");
str.Format(_T("%s%d"), str, 123);
Read up on _T and <tchar.h> here: Generic-Text Mappings in Tchar.h .

sprintf ( OutputBuffer, "%s%d", str, 123 ) ;
Use CStringA for ANSI Version.
Use CStringW for Unicode Version.
Use CString for TCHAR Version.
To force CString to be evaluated as CStringA::
Goto, Project->Properties->Configuration Properties->General.
On right hand side, you will get "Character Set" Row, change that to "Not set"

Related

extern "C" 'initializing': cannot convert from 'const char [1]' to 'char *'

I have strange error when compiling open source lib
I using visual studio 2019
and I created a solution to the lib cmake -G "Visual Studio 16 2019" ../"
but I keep getting erros when compiling :
starting with this line :
CFUNC char *optarg = "";
C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(16,24): error C2440: 'initializing': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(16,20): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(30,27): error C2440: 'initializing': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(30,21): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(38,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(38,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(45,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(45,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(51,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(51,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(85,16): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(85,12): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(94,15): error C2440: '=': cannot convert from 'const char [1]' to 'char *'
1>C:\Dev\my\cpp\libs\libunistd-master\unistd\getopt.cpp(94,11): message : Conversion from string literal loses const qualifier (see /Zc:strictStrings)
"" is a constant string. You can't just assign that to a non-constant char *, because then it could be mutated through the pointer.
According to the source code of libunistd, the implementation was taken from NetBSD, but a quick check¹ shows that NetBSD doesn't initialize optarg at all:
char *optarg; /* argument associated with option */
You could follow the same approach.
You could also turn off this error by passing /Zc:strictStrings- (note the hyphen) to your compiler. As long as the constant string isn't written to, there should be little harm in that.
You might want to report this as a bug to the library author.
¹ Not sure I'm looking at the latest version. It's from 2014.
It's not strange. The type of "" (or any string literal) is const char[] and the type of optarg is char*. So if the assignment was allowed you would be able to modify a string literal via the pointer optarg. Clearly that's not desireable.
In an older version of C++, this assignment was allowed as a special case, simply because there was too much old C code that did this. But this is no longer the case.

'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'

I'm still rather new to C++ but I've ran into a problem I can't solve, this is my error message:
'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'
And this the line that is throwing the error:
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
You are passing a char * to something that needs a wchar_t *. You'll have to either convert your stringtype to wchar_t *, for example using the MultiByteToWideChar function (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx), or you can use the non-wide version of GetModuleHandle by calling GetModuleHandleA() instead of GetModuleHandleW().

C++03: _ui64toa_s: error C2446: ':' : no conversion from 'errno_t' to 'const char *'

I have the following line of code:
(total_time== 0xAAAABBBB) ? ("") : _ui64toa_s(total_time, myArray, 1024, 10);
As the title suggests, I receive this error:
error C2446: ':' : no conversion from 'errno_t' to 'const char *'
I am unsure how to convert errno_t to const char *.
I tried to solve it by writing
const char *n = _ui64toa_s(total_time, myArray, 1024, 10);
only to see this:
error C2440: 'initializing' : cannot convert from 'errno_t' to 'const char *'
_ui64toa_s does not return a string like _ui64toa does.
Instead it returns an errno_t which is defined as int.
errno_t e = _ui64toa_s(total_time, myArray, 1024, 10);
But since you're using C++ I recommend you use std::to_string instead.
std::to_string(total_time).c_str();
You might also want to take a look at this question.

'default argument' : cannot convert from 'const char [1]' to 'const wchar_t *'

I'm trying to compile some old game SRC I found on the net here's the code
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
Change _T("blah") to L"blah".
_T is a macro that does nothing or adds an L.
Alternatively, compile project with the wchar option for _T and TCHAR.
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
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
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.

How can I use SimpleIni to return wchar_t and then convert to std::wstring?

SimpleIni Documentation says wchar_t is supported but I don't understand how to use it. This is what I tried:
CSimpleIniCaseW ini;
ini.LoadFile("myapp.ini");
std::wstring test(ini.GetValue("testsection", "testkey", ""));
error C2664:
'CSimpleIniTempl::GetValue'
: cannot convert parameter 1 from
'const char [12]' to 'const wchar_t *'
You need to specify your strings as L"testsection" << Note the L.