Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using wait() and fork() in c++

Tags:

c++

c

fork

wait

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?

like image 679
MOHAMED Avatar asked Dec 26 '22 05:12

MOHAMED


1 Answers

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>
like image 200
Mike Seymour Avatar answered Jan 05 '23 06:01

Mike Seymour