I'm trying to use wait()
and fork()
in my c++ code. but I get the following error in the compilation phase
../test/my_test.cpp: In member function 'void MYClass::myMethod()':
../test/my_test.cpp:98: error: no matching function for call to 'wait::wait(int*)'
/data/backfire/staging_dir/toolchain-i386_gcc-4.1.2_uClibc-0.9.30.1/lib/gcc/i486-openwrt-linux-uclibc/4.1.2/../../../../i486-openwrt-linux-uclibc/sys-include/bits/waitstatus.h:68: note: candidates are: wait::wait()
/data/backfire/staging_dir/toolchain-i386_gcc-4.1.2_uClibc-0.9.30.1/lib/gcc/i486-openwrt-linux-uclibc/4.1.2/../../../../i486-openwrt-linux-uclibc/sys-include/bits/waitstatus.h:68: note: wait::wait(const wait&)
Code:
void MYClass::myMethod()
{
pid_t pid;
int status;
if ((pid = fork()) < 0) {
printf("error fork\n");
return;
}
if (pid == 0) {
/* cild*/
......
}
/*parent*/
while (wait(&status) != pid);
}
How to fix the error?
The error indicates that it was trying to create an object of a type called wait
, converting from a pointer, rather than (as expected) trying to call a function called wait
.
The problem is that you haven't included the header that declares the wait
function. However, there is a type called wait
defined in another header so, without the function declaration, the compiler assumes you mean that.
Solution, from the manpage for wait(2)
:
#include <sys/types.h>
#include <sys/wait.h>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With