2011-09-06 25 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