C++等待问题

4
我有一段类似这样的代码。
    if (pid > 0) {
        // Child    
    } else {
        // Parent
    }

while (wait() > 0) {}

还有包含文件

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

但是当我尝试使用g++编译它(g++ test.cpp -o test)时,出现了错误:

lab3.cpp: In function «int main(int, char**)»:
lab3.cpp:57:18: error: no match for «operatorin «{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>&)

我做错了什么?

1
wait()被定义在sys/wait.h中,但如果只有这个缺失,我不明白为什么你会出现这样的错误。 - Mat
2
你做错的是,你没有告诉我们你代码片段中第57行是什么。 - Nordic Mainframe
如果可以的话,请提供完整的源代码(至少对于该文件)。我认为你还有其他我们看不到的问题。 - John Humphreys
2个回答

4

您的代码中间接包含了 /usr/include/bits/waitstatus.h,其中定义了一个 union wait。由于您没有 wait 函数的声明,C++ 将 wait() 视为构造函数表达式。这意味着 wait()>0 调用了一个不存在的 operator>(const wait &,int)。 GCC 的诊断信息对此并不真正有帮助。请添加 sys/wait.h 以获取 wait 函数的有效原型。


2

wait() 是在 sys/wait.h 中定义的。

但是,可能还有其他我们无法从您发布的代码中看到的问题。

尝试将其更改为:

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

确保您从全局命名空间中调用wait()函数,而不是从其他从别处导入的类或结构中调用。


也许对于原帖作者,使用 Do 会更好,如果他确实想要至少运行一些代码的话。 - sqlmole

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接