Pointer compatibility from VS2015 to VS2008 - c++

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 ?

Related

Casting error converting from unsigned int to uint16_t

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();

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

error C2664: 'imgGrab' : cannot convert parameter 2 from 'Int8 **' to 'void **'

I am writing an x64 DLL using Visual Studio 2008.
The compiler error
error C2664: 'imgGrab' : cannot convert parameter 2 from 'Int8 **' to 'void **'
occurs at this line;
Int8* ImaqBuffer;
Int32 err = imgGrab (Sid, &ImaqBuffer, TRUE);
I've managed to get it to compile by doing;
Int32 err = imgGrab (Sid, (void **)&ImaqBuffer, TRUE);
but I think this is wrong. Am I right in thinking this is wrong? If so how do I fix it. I suspect its a compiler setting in Visual Studio because the code compiles ok in the sample project from the manufacturer.

C++ Missing type specifier: syntax error

I hook a function with windows detours in C++.
I get an error in following code:
void (*asmFunction)(const char *text);
void hookFunction(const char *text) {
__asm nop;
asmFunction(text);
}
asmFunction = (void (__cdecl *)(const char *))DetourFunction((PBYTE)0x433A90, (PBYTE)&hookFunction);
The compiler (MSVC++ 2008) says:
error C4430: Missing type specifier - int assumed. Hint: "default-int" is not supported in C++. Yadda yadda …
error C2373: 'asmFunction': redefinition with different specifiers
error C2440: 'in initialization': 'void (__cdecl *)(const char *)' cannot be converted to 'int'. There is no context in which this conversion is valid.
The code worked yesterday. What's wrong with it? How can I fix it without destructing the hook?
This expression needs to be within a function, e.g.
int main() {
asmFunction = (void (__cdecl *)(const char *))DetourFunction(
(PBYTE)0x433A90, (PBYTE)&hookFunction
);
// ...
}
Go read a book on C++.

question about qsort implementation

i have implement this code from programming pearls and i think it should be correct but it gives me this mistake
code:
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
using std::qsort;
int charcmp(char*x,char *y){ return *x-*y;}
#define wordmax 100
int main(void){
char word[wordmax];
char sig[wordmax];
while(scanf("%s",word)!=EOF){
strcpy(sig,word);
qsort(sig,strlen(sig),sizeof(char),charcmp);
printf("%s %s\n",sig,word);
}
return 0;
}
mistake:
1>------ Build started: Project: anagrams, Configuration: Debug Win32 ------
1> anagrams.cpp
1>c:\users\david\documents\visual studio 2010\projects\anagrams\anagrams.cpp(11): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\david\documents\visual studio 2010\projects\anagrams\anagrams.cpp(13): error C2664: 'qsort' : cannot convert parameter 4 from 'int (__cdecl *)(char *,char *)' to 'int (__cdecl *)(const void *,const void *)'
1> None of the functions with this name in scope match the target type
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
i think jon bentley should know such kind of topic yes why is such kind of mistake?
Your charcmp function needs to take const void* parameters:
int charcmp(const void* x, const void* y)
{
return *(const char*)x - *(const char*)y;
}
The error message:
cannot convert parameter 4 from 'int (__cdecl *)(char *,char *)' to 'int (__cdecl *)(const void *,const void *)'
is telling you that the argument you are passing (a pointer to the function charcmp) does not have the correct type to be passed into qsort.
Since this question is tagged as C++, you might consider using std::sort instead; it is type safe and much easier to use:
std::sort(sig, sig + strlen(sig));
The error to lookout is
int (__cdecl *)(char *,char *)' to 'int (__cdecl *)(const void *,const void *)'
The function expects arguments of type const void*.