【问题标题】:C++ wait problemC++ 等待问题
【发布时间】:2011-11-11 13:10:37
【问题描述】:

我有这样的代码

    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 «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>&)

我做错了什么?

【问题讨论】:

  • wait()sys/wait.h 中定义,但我不明白为什么你会收到这样的错误,如果这是唯一缺少的东西。
  • 您做错的是,您没有告诉我们您的 sn-p 中的第 57 行是什么。
  • 如果可以的话,提供完整的源代码(至少对于那个文件)。我认为您还有其他我们看不到的事情发生。

标签: c++ g++ ipc fork wait


【解决方案1】:

wait()sys/wait.h 中定义。

但您发布的代码中可能还有其他一些我们看不到的东西。

尝试将其更改为:

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

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

【讨论】:

  • 对于 OP 也可能使用 Do,如果他确实想至少运行一次代码,他的实现可能会更好。
【解决方案2】:

你的间接包括拉/usr/include/bits/waitstatus.h,它定义了一个union wait。由于您没有等待函数的声明,因此 C++ 将 wait() 视为构造函数表达式。这意味着wait()&gt;0 要求operator&gt;(const wait &amp;,int) 当然不存在。 GCC 的诊断在这里并没有真正的帮助。添加sys/wait.h 以获取等待函数的有效原型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2021-05-24
    • 2021-10-11
    • 2014-10-27
    相关资源
    最近更新 更多