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.
Related
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.
I have added sqlite3.c file into my project.
And #include. Here is the code:
#include <sqlite3.h>
using namespace std;
int main()
{
return 0;
}
I compile the program and it throws the following error:
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(15705): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(19741): error C2440: '=' : cannot convert from 'void *' to 'sqlite3_mutex *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(20665): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(20677): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(21142): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(21256): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>..\..\..\..\..\..\..\libraries\sqlite-amalgamation-3080702\sqlite3.c(21411): error C2440: '=' : cannot convert from 'void *' to 'char *'
But there is no error that a header or file is not found. Everything is found and the errors are just outputted above
I have found the solution. I compiled the C code as C++, but changing it was not enough.
I write it for future visitors:
First, I had to change file's (ONLY FILE) property. Right-click on the file and select properties, under the C/C++, select Advanced and then select Compile As and set it to C (neither default nor C++).
Then, you should make sure that your .c file is compiled without clr. Well, to do that, under the same C/C++ set of menu, select "Common Langugae Runtime Support" and set it to No Support....
I'm using Visual Studio 17 and I would add to this, find the settings for "Precompiled Header" and set that to "Not Using Precompiled Headers".
I am new to VC++ 2012. I have this code snippet.
auto it = query_map.find(U("callback"));
The problem is right under the dot there is a red line the error is
Error 1 error C2664: 'std::_Tree_iterator std::_Tree::find(const http::uri::encoded_string &)' : cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &' d:\maverick\projects\strikeforce\src\server\server\server.cpp 26
Can someone tell me a solution to this error?
Error 1 error C2664: 'std::_Tree_iterator std::_Tree::find(const http::uri::encoded_string &)'
This is a problem with the method find() from the class std::Tree
cannot convert parameter 1 from 'const wchar_t [9]' to 'const http::uri::encoded_string &'
The find() method must be used with a paremeter of type 'const http::uri::encoded_string &', but you passed a 'const wchar_t [9]'.
the method U() you have used returns an array of chars, but the find() method need another type of object.
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.
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"