Casting error converting from unsigned int to uint16_t - c++

Why do I get this error
1>c:\users\g\documents\visual studio
2012\projects\tlvdemo\tlvdemo\tlvobject.cpp(50): error C2440: 'type
cast' : cannot convert from 'unsigned int (__thiscall
std::basic_string<_Elem,_Traits,_Alloc>::* )(void) throw() const' to
'uint16_t'
and how to fix it?
Code
uint16_t tagLen = (uint16_t)m_tagName.length; //m_tagName is string type

The problem is with the way you are calling the length method.
You have to use m_tagName.length();

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.

Pointer compatibility from VS2015 to VS2008

In a header file that I cannot modify, I have:
void SetReceiveCallback(void (*funcRXReceiveDataptr)(char* data, int length));
In my cpp code, I have:
void pcsocketcommunication::receiveData(char* data, int length)
{
}
communication = new CommunicationInterface(true, "192.168.214.1", 41682, 2000000, 41681, 2000000);
communication->SetReceiveCallback((void *)(&pcsocketcommunication::receiveData));
I can compile with VS2015 but VS2008 says:
error C2440: 'type cast' : cannot convert from 'void (__thiscall pcsocketcommunication::* )(char *,int)' to 'void *'
How can I modify the calling function to be able to compile in VS2008 ?

Compiling SQLite with Visual Studio C++ 2013 Throws error for .c file

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".

Initializing auto variables in VC++ 2012

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.

Find method in std::wstring

I have declared Wstring as follows
wstring strID
When I try to find the occurrences sub-string as follows
int index = strID.find("LABS");
I am getting error like the following
error C2664: 'unsigned int std::basic_string<_Elem,_Traits,_Ax>::find(const std::basic_string<_Elem,_Traits,_Ax> &,unsigned int) const' : cannot convert parameter 1 from 'const char [13]' to 'const std::basic_string<_Elem,_Traits,_Ax> &'
Can you please help me to find the occurrences of sub-string?
When searching a wstring, you need to have the parameter as a wide string as well
int index = strID.find(L"LABS");
^
int index = strID.find(L"LABS");
EDIT:
http://msdn.microsoft.com/en-us/library/69ze775t(v=vs.80).aspx