I am trying to compile some code that was given to me that I'm told compiles fine. Perhaps on a different compiler. I am using VS2010 and I have the following line:
char *dot = strrchr(filename, '.');
This causes the compiler error:
"error C2440: 'initializing': cannot convert from 'const char *' to
'char *'
How come? And how do I fix it?
The error message is pretty clear. strrchr returns a const char*. So you need:
const char *dot = strrchr(filename, '.');
If you really need a char*, you can use strcpy for conversion.
C++ has saner versions of strchr and strrchr than C thanks to overloading, so say:
const char * dot = strrchr(filename, '.');
In C, which has no overloading, you only have a single function char * strrchar(const char *, const char *), and it's up to you to decide whether the result is constant or mutable, depending on which type of pointer to feed into the function. C has many such type-unsafe functions.
Related
I am trying to import a C library into my C++ project but I'm stuck with this error
invalid conversion from 'void*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]
uint8_t const* raw = static_cast(getPointerToData(id, message->data, message->length));
^
compilation terminated due to -Wfatal-errors.
This code compile well using C compiler but get this error using C++ one
bool XbusMessage_getDataItem(void* item, enum XsDataIdentifier id, struct XbusMessage const* message)
{
uint8_t const* raw = (getPointerToData(id, message->data, message->length));
Can you help me?
Thanks
You need to explicitly cast the return value from getPointerToData to const uint8_t*. Implicit conversions between pointer types are not allowed in C++ but are in C.
Try:
uint8_t const* raw = static_cast<uint8_t const*>(getPointerToData(id, message->data, message->length));
Edit:
You can also use a C-style cast if you want to keep the C code C and not C++.
uint8_t const* raw = (uint8_t const*)(getPointerToData(id, message->data, message->length));
This code posted by Jean-François Fabre works
uint8_t const* raw = (getPointerToData(id, static_cast<const uint8_t*>(message->data), message->length));
Thanks!
I have some old C++ file which I know used to compile. I have created a new install of Visual C++ version 6.
I am getting lots of compile errors with CStrings about not being able to convert to const char *
Here's an example.
CString dogs = "test";
writeoutfile(dogs, 1);
void Crender::writeoutfile(CString data, long data_size) {}
I get this error:
error C2664: 'void __thiscall Crender::writeoutfile(const char *,long)' : cannot convert parameter 1 from 'class CString' to 'const char *'
Is there some way I can get round this?
You have to get the raw pointer to the char field. This can be done with
CString::GetBuffer()
so you could call
writeoutfile(dogs.GetBuffer(), 1);
CString should convert to const char*. Is it a Unicode build? That's the only explanation I can think of.
GetBuffer() is for getting a writeable pointer to the data contained inside CString. Don't do that!
I'm trying to expose a overloaded function using boost::python.
the function prototypes are:
#define FMS_lvl2_DLL_API __declspec(dllexport)
void FMS_lvl2_DLL_API write(const char *key, const char* data);
void FMS_lvl2_DLL_API write(string& key, const char* data);
void FMS_lvl2_DLL_API write(int key, const char *data);
I'v seen this answer: How do I specify a pointer to an overloaded function?
doing this:
BOOST_PYTHON_MODULE(python_bridge)
{
class_<FMS_logic::logical_file, boost::noncopyable>("logical_file")
.def("write", static_cast<void (*)(const char *, const char *)>( &FMS_logic::logical_file::write))
;
}
results with the following error:
error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type
trying the following:
void (*f)(const char *, const char *) = &FMS_logic::logical_file::write;
results:
error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'void (__cdecl *)(const char *,const char *)'
None of the functions with this name in scope match the target type
what's wrong and how to fix it?
EDIT
I forgotten to mention a few things:
I'm using vs2010 pro on win-7
write is a member function of logical_file
FMS_logic is a namespace
Well the second attemp should work, if write is a pure function. From your code it seems you do have a memberfunction. Pointers to member-functions are ugly, you'd rather use a function object.
However: you would have to post the whole code, it is not clear whether write is a member-function or not.
Edit: if it is a member-function of FMS_logic::logical_file the syntax would be:
void (FMS_logic::logical_file::*f)(const char *, const char *) = &FMS_logic::logical_file::write;
This just applies for non-static member function, i.e. if a function is static or logical_file is just a namespace it is as you wrote it before.
Your code doesn't work because your function pointer type is wrong. You need to include all type qualifiers (your DLL qualifier is missing) and, as Klemens said, the class name. Putting this together, your code should read
.def("write", static_cast<void FMS_lvl2_DLL_API
(FMS_logic::logical_file::*)(const char *, const char *)>
(&FMS_logic::logical_file::write))
Thanks for the hint with the static_cast<>, I had the same problem as you, just without the dllexport, and after adding the static_cast it works :-)
im developin QT application, and i need to include pure C code. When i compile this code in code::blocks it was successful, maybe one warning, but when i try to compile it in QT creator, i get these 4 errors.
cannot convert 'char*' to 'WCHAR*' for argument '1' to 'UINT GetSystemDirectoryW(WCHAR*, UINT)'
cannot convert 'char*' to 'const WCHAR*' for argument '1' to 'HINSTANCE__* LoadLibraryW(const WCHAR*)'
cannot convert 'char*' to 'WCHAR*' for argument '1' to 'BOOL
cannot convert 'const char*' to 'const WCHAR*' for argument '2' to 'LONG RegQueryValueExW(HKEY__*, const WCHAR*, DWORD*, DWORD*, BYTE*, DWORD*)'
and the code is here>
char systemDirectory[MAX_PATH];
GetSystemDirectory(systemDirectory, MAX_PATH); //first error
char kbdLayoutFilePath[MAX_PATH];
kbdLibrary = LoadLibrary(kbdLayoutFilePath); //second error
char kbdName[KL_NAMELENGTH];
GetKeyboardLayoutName(kbdName); //third error
if(RegQueryValueEx(hKey, "Layout File", NULL, &varType, layoutFile, &bufferSize) != ERROR_SUCCESS) //fourth error
i also use snprintf function, so i cant just change the type from char to WCHAR, because then it wont compile the snprintf
snprintf(kbdKeyPath, 51 + KL_NAMELENGTH,
"SYSTEM\\CurrentControlSet\\Control\\Keyboard Layouts\\%s", kbdName);
So do you have any ideas how to fix it ? first i tried change type from char to WCHAR, but then the snprintf didnt work, so i tried to use swprinf, but with no success, since strangely it didnt find this function
int swprintf(wchar_t *wcs, size_t maxlen,
const wchar_t *format, ...);
but just this
int swprintf(wchar_t *wcs,
const wchar_t *format, ...);
so what are my option ? How to compile pure C code in c++ environment without any errors... or how to make the right type conversion.
You are compiling in Unicode mode. You could set your compile to multi-byte strings. The problem that is happening is those windows API functions are macros that check whether you are building Unicode or not and then call either the W or A version of the function (in your code there, the GetSystemDirectory is actually calling GetSystemDirectoryW. So, you can either change your compile to multi-byte strings....or you could explicitly change your api calls to call the A version (i.e. GetSystemDirectoryA)
You are compiling your project with the UNICODE or _UNICODE define. Check your project settings and remove the define if necessary. To remove the define, you might need to disable unicode support for the whole project.
Change over from char to WCHAR and then to solve your swprintf problem just do this
#define swprintf _snwprintf
On Windows, the prototype of swprintf is
int swprintf( wchar_t *buffer,const wchar_t *format [,argument] ... );
But the ISO C Standard requires the following prototype for swprintf
int swprintf (wchar_t *, size_t, const wchar_t *, ...);
For this very reason, on Windows, _snwprintf is provided.
Read this for more details
http://msdn.microsoft.com/en-us/library/ybk95axf(v=vs.71).aspx
I am trying to compile the following code in my test application on windows in visual studio for C++:
const wchar_t* chinese = "好久不见";
But I get the following error:
error C2440: 'initializing' : cannot convert from 'const char [5]' to 'const wchar_t *
I am compiling with unicode, so I am confused about this. The error goes away if I cast the literal like this:
const wchar_t* chinese = (wchar_t*)"好久不见";
I am not sure that is safe nor do I really want to do that so how can I fix this.
Thank you!
You want a wide string literal, so prefix the string literal with L:
const wchar_t* chinese = L"好久不见";
Like GMan said, prefix the string with an 'L'. The L identifies the string as a wide char type (wchar_t).
const wchar_t* chinese = L"好久不见";