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*.
Related
I'm trying to figure out a problem I have in my project, and I have simplified it down to this little bit of code that generates the C2664 error. I don't understand the error message, could anyone help me to understand? I've googled, and I've looked through 2 C++ books and this code is exactly what is listed in them, but it does not work for me.
Thanks.
#include <memory>
struct A
{
int b;
};
int main(int argc, char ** argv)
{
A a;
std::unique_ptr<A> a_ptr = std::make_unique<A>(new A);
return 0;
}
And here is the error:
1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>main.cpp
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2585): error C2664: 'A::A(const A &)': cannot convert argument 1 from 'A *' to 'A &&'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2584): note: Reason: cannot convert from 'A *' to 'A'
1>c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.12.25827\include\memory(2584): note: No constructor could take the source type, or constructor overload resolution was ambiguous
1>d:\users\aksel\documents\visual studio 2017\projects\project1\project1\main.cpp(21): note: see reference to function template instantiation 'std::unique_ptr<A,std::default_delete<_Ty>> std::make_unique<A,A*,0>(A *&&)' being compiled
1> with
1> [
1> _Ty=A
1> ]
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The argument to make_unique is the argument to a T constructor, not a pointer to a T instance, just use the regular unique_ptr ctor if you already have a pointer to a T.
I am trying to use a pthread library.
What is the cause of this error?
error C2664: 'pthread_create' : cannot convert parameter 3 from 'void *(__clrcall *)(void *)' to 'void *(__cdecl *)(void *)
Here's my code:
pthread_create(&thread1,NULL,sub_m,(void *)data);
void *sub_m(void* data)
make it void __cdecl sub_m(void *data). You are in managed code, so you need to get back into the proper calling convention.
This question already has answers here:
problem sorting using member function as comparator
(9 answers)
Closed 7 years ago.
Im trying to sort vector of structs. I saw this and this examples.
this is my code:
.h file:
// I didn't mention all the includes and namespace
class fileLoader
{
struct commands
{
int time;
string name;
};
commands resultStruct;
vector<commands> resultVector
private:
void sortFunction();
bool compareByTime(const commands &a, const commands &b);
}
.cpp file:
void fileLoader::sortResults()
{
sort(resultVector.begin(), resultVector.end(), compareByTime);
}
bool fileLoader::compareByTime(const commands &a, const commands &b)
{
return a.time < b.time;
}
this is the compilation error I get:
error C3867: 'fileLoader::compareByTime': function call missing argument list; use '&fileLoader::compareByTime' to create a pointer to member
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3639) : see declaration of 'std::sort'
when I tried to change compareByTime to &fileLoader::compareByTime, I got this compilation error:
error C2064: term does not evaluate to a function taking 2 arguments
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3776) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Unguarded_partition<_RanIt,_Pr>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Ty1=fileLoader::commands *,
1> _Ty2=fileLoader::commands *,
1> _RanIt=fileLoader::commands *,
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3806) : see reference to function template instantiation 'void std::_Sort<fileLoader::commands*,__w64 int,_Pr>(_RanIt,_RanIt,_Diff,_Pr)' being compiled
1> with
1> [
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &),
1> _RanIt=fileLoader::commands *,
1> _Diff=__w64 int
1> ]
std::sort<std::_Vector_iterator<_Myvec>,bool(__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)>(_RanIt,_RanIt,_Pr)' being compiled
1> with
1> [
1> _Myvec=std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>,
1> _RanIt=std::_Vector_iterator<std::_Vector_val<fileLoader::commands,std::allocator<fileLoader::commands>>>,
1> _Pr=bool (__thiscall fileLoader::* )(const fileLoader::commands &,const fileLoader::commands &)
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(3720): fatal error C1903: unable to recover from previous error(s); stopping compilation
1>
1>Build FAILED.
I will be happy for some help.
thanks.
First fix all the typos (missing semicolons etc).
Then change your sorting function to static bool compareByTime(const commands &a, const commands &b);
Short answer
Make compareByTime static (or 'global' outside class)
Explanation
Member functions require this to be passed to them somehow, so two argument member function is probably 3 argument function. So compiler can't use it when it needs 2 arguments comparator.
I'll be very pleased if you help.
My IDE is VS2010.
I'm using boost 1.47.0, especially boost::asio.
After some days of developing I decided to add log4cxx.
log4cxx needs to change calling convention to __stdcall
I surprisingly got lots of compiling error. They are ~ 70 errors.
I've googled a bit and found these:
#define BOOST_BIND_ENABLE_STDCALL
#define BOOST_MEM_FN_ENABLE_STDCALL
It helps. Now there are just ~10 errors.
Here there are:
1>ClCompile:
1> main.cpp
1>D:\Development\lib\boost_1_47_0\boost/detail/interlocked.hpp(61): error C2373: '_InterlockedCompareExchange' : redefinition; different type modifiers
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\intrin.h(214) : see declaration of '_InterlockedCompareExchange'
1>D:\Development\lib\boost_1_47_0\boost/detail/interlocked.hpp(62): error C2373: '_InterlockedExchange' : redefinition; different type modifiers
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\intrin.h(192) : see declaration of '_InterlockedExchange'
1>D:\Development\lib\boost_1_47_0\boost/detail/interlocked.hpp(63): error C2373: '_InterlockedExchangeAdd' : redefinition; different type modifiers
1> C:\Program Files\Microsoft Visual Studio 10.0\VC\include\intrin.h(204) : see declaration of '_InterlockedExchangeAdd'
1>D:\Development\lib\boost_1_47_0\boost/smart_ptr/detail/sp_counted_base_w32.hpp(92): error C2446: '==' : no conversion from 'long' to 'long (__stdcall *)(volatile long *,long,long)'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>D:\Development\lib\boost_1_47_0\boost/smart_ptr/detail/sp_counted_base_w32.hpp(92): error C2040: '==' : 'long (__stdcall *)(volatile long *,long,long)' differs in levels of indirection from 'long'
1>D:\Development\lib\boost_1_47_0\boost/smart_ptr/detail/sp_counted_base_w32.hpp(92): error C3861: '_InterlockedCompareExchange': identifier not found
1>D:\Development\lib\boost_1_47_0\boost/smart_ptr/detail/spinlock_w32.hpp(62): error C3861: '_InterlockedExchange': identifier not found
1>D:\Development\lib\boost_1_47_0\boost/smart_ptr/detail/spinlock_w32.hpp(62): error C2440: 'initializing' : cannot convert from 'long (__stdcall *)(volatile long *,long)' to 'long'
1> There is no context in which this conversion is possible
1>D:\Development\lib\boost_1_47_0\boost/asio/detail/impl/signal_set_service.ipp(74): error C2664: 'signal' : cannot convert parameter 2 from 'void (__stdcall *)(int)' to 'void (__cdecl *)(int)'
1> None of the functions with this name in scope match the target type
1>D:\Development\lib\boost_1_47_0\boost/asio/detail/impl/signal_set_service.ipp(246): error C2664: 'signal' : cannot convert parameter 2 from 'void (__stdcall *)(int)' to 'void (__cdecl *)(int)'
1> None of the functions with this name in scope match the target type
1>main.cpp(20): warning C4007: 'main' : must be '__cdecl'
How can I solve them?
Any little ideas or hints?
You also need
#define BOOST_USE_WINDOWS_H
and possibly /Gz (__stdcall)
If your code looks something like:
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/helpers/exception.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
int main()
{
//stuff
}
and you compile with -llog4cxx then you should be fine.
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++.