2011-09-06 76 views
4

我有这样C++等问题

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

while (wait() > 0) {} 

一个代码,并有包括

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

但是,当我尝试使用g compiile它++(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>&) 

我做错了什么?

+1

'wait()'在'sys/wait.h'中定义,但我不明白为什么你会收到这样的错误,如果这是唯一缺少的东西。 – Mat

+2

你在做什么错,是你没有告诉我们你的代码片段中的第57行。 –

+0

如果可以的话(至少为那个文件)提供完整的源代码。我想你还有别的事情我们不能在那里看到。 –

回答

4

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

2

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

但是可能还有别的事情发生,我们无法看到您发布的代码。

尝试改变,为:

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

,以确保你从全局命名空间中调用wait()功能,而不是其他一些类或从其他地方进口的构建体。

+0

同样对于OP来说可能使用Do,如果他确实想要至少运行一些代码,可能会更好。 – sqlmole