? :操作符要求返回的是一个有类型的操作数,而endl不是,而‘\n'是一个字符型操作数。看看MSDN给EDNL的解释吧! Standard C++ Library Reference endlSee Also <ostream> Members Terminates a line and flushes the buffer.template class<_Elem, _Tr> basic_ostream<_Elem, _Tr>& endl( basic_ostream<_Elem, _Tr>& _Ostr ); Parameters _Elem The element type. _Ostr An object of type basic_ostream. _Tr Character traits. Return Value An object of type basic_ostream.Res The manipulator calls _Ostr.put(_Ostr. widen('\n')), and then calls _Ostr.flush. It returns _Ostr.
i==0?' ':endl三目运算符的三个条件只能用变量、常量或语句(准确地说,是语句的结果,其实也是一个数), endl 不是变量又不是语句. 如果你还不明,那你想想其它运算符吧, 运算符的左右都是一个数(变量 or 常量 or 语句),三目运算符也是运算符之一,当然要付合这条规则了.
Standard C++ Library Reference endlSee Also
<ostream> Members
Terminates a line and flushes the buffer.template class<_Elem, _Tr>
basic_ostream<_Elem, _Tr>& endl(
basic_ostream<_Elem, _Tr>& _Ostr
);
Parameters
_Elem
The element type.
_Ostr
An object of type basic_ostream.
_Tr
Character traits.
Return Value
An object of type basic_ostream.Res
The manipulator calls _Ostr.put(_Ostr. widen('\n')), and then calls _Ostr.flush. It returns _Ostr.