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

16,477

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>
Share:
16,477
MOHAMED
Author by

MOHAMED

Contact me on LinkedIn.

Updated on June 12, 2022

Comments

  • MOHAMED
    MOHAMED almost 2 years

    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?