Problem with C ++ wait

I have code like this

if (pid > 0) { // Child } else { // Parent } while (wait() > 0) {} 

And there is

 #include <cstdlib> #include <iostream> #include <cstdio> #include <ctime> #include <sys/types.h> 

But when I try to compile it with g ++ ( g++ test.cpp -o test ) a, there are errors:

 lab3.cpp: In function «int main(int, char**)»: lab3.cpp:57:18: error: no match for «operator>» in «{0} > 0» lab3.cpp:57:18: warning: candidates are: /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_pair.h:220:67: : template<class _T1, class _T2> bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_iterator.h:304:46: : template<class _Iterator> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_iterator.h:354:47: : template<class _IteratorL, class _IteratorR> bool std::operator>(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2548:58: : template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2560:27: : template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2572:58: : template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) /usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/stl_vector.h:1303:77: : template<class _Tp, class _Alloc> bool std::operator>(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&) 

What am I doing wrong?

+4
source share
2 answers

In your indirect one, tap /usr/include/bits/waitstatus.h , which defines union wait . Since you don't have a wait function declaration, C ++ considers wait () as a constructor expression. This means that wait()>0 calls operator>(const wait &,int) , which does not exist, of course. Diagnosing GCC here is not very helpful. Add sys/wait.h to get a valid prototype of the wait function.

+4
source

wait() is defined in sys/wait.h

But maybe something else is going on there that we cannot see with the code you posted.

Try changing this to:

 while (::wait() > 0) ... 

to make sure you call the wait() function from the global namespace, and not some other class or construct imported from other sources.

+2
source

All Articles