c++等待问题
C++ wait problem
我有一个这样的代码
if (pid > 0) {
// Child
} else {
// Parent
}
while (wait() > 0) {}
还有include
#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>&)
我做错了什么?
您的include间接拉取/usr/include/bits/waitstatus.h
,这定义了一个union wait
。由于没有wait函数的声明,c++将wait()视为构造函数表达式。这意味着wait()>0
调用一个当然不存在的operator>(const wait &,int)
。GCC的诊断在这里并没有真正的帮助。添加sys/wait.h
来获取等待函数的有效原型
wait()
是在sys/wait.h
中定义的。
但是这里可能有一些我们无法从你发布的代码中看到的东西。
试着把它改成:
while (::wait() > 0) ...
确保从全局命名空间调用wait()
函数,而不是从其他地方导入的其他类或构造。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 在 c++ 中等待一个子进程与另一个子进程时遇到问题
- 解决死锁问题,在主线程中等待多个工作线程完成 (C++11)
- C++等待和睡眠问题
- c++等待问题
- MPI 中的非阻塞通信和 MPI 等待问题.并非所有信息都正确传递
- 如何解决多个运行线程的繁忙等待问题
- 性能问题与c++(使用vc++ 2010):在运行时,我的程序似乎随机等待一段时间