三元运算符C ++ Ostream to String不兼容的操作数

时间:2014-09-30 04:58:23

标签: c++ c++11 ternary-operator nested ostream

return (dayCheck = false) ? cout << "On the " << day << 
"th Day of Christmas, My True Love Gave to Me: " 
<< endl << Christmas(day, count, true) : 
          (count != 0) ? cout << arr[count] << endl 
          << Christmas(day, count - 1, true) : 
                  Christmas(day + 1, day + 1, false);

我试图做一个嵌套的三元运算符,但是我无法找到一种方法来使嵌套运算符中的操作数兼容,一个是ostream,另一个是字符串(char)。是否有一种方法来转换它还是有另一种方法我需要格式化它以保持嵌套? (对于这个特定的代码,我实际上专注于三元运算符的嵌套)

4 个答案:

答案 0 :(得分:1)

您错过了cout

此外,当您在适当的位置添加paranthesis时,它会使代码更具可读性。

使用:

return (dayCheck = false) ?

    // Add a pair of paranthesis for the first statement.
    (cout << "On the " << day << "th Day of Christmas, My True Love Gave to Me: " << endl << Christmas(day, count, true)) : 
    (count != 0) ?

    // Add a pair of paranthesis for the next first statement.
    (cout << arr[count] << endl << Christmas(day, count - 1, true)) : 

    // Add a pair of paranthesis for the last statement.
    (cout << Christmas(day + 1, day + 1, false));
  // ^^^^ The missing cout

答案 1 :(得分:0)

问题在于第二个三元表达式。这两种情况不具有相同的返回类型。 第一个返回ostream对象

cout << arr[count] << endl << Christmas(day, count - 1, true)

,第二个返回Christmas返回

Christmas(day + 1, day + 1, false)

要解决此问题,请使第二种情况也返回ostream对象:

return (dayCheck = false) ? cout << "On the " << day << 
"th Day of Christmas, My True Love Gave to Me: " 
<< endl << Christmas(day, count, true) : 
          (count != 0) ? cout << arr[count] << endl 
          << Christmas(day, count - 1, true) : 
                  cout << Christmas(day + 1, day + 1, false);

答案 2 :(得分:0)

return (dayCheck == false) ?
        (cout << "On the " << day << "th Day of Christmas, My True Love Gave to Me: " << endl << Christmas(day, count, true)) :

        (count != 0) ? cout << arr[count] << endl << Christmas(day, count - 1, true) :

        (cout << Christmas(day + 1, day + 1, false));

所以这似乎解决了问题,而圣诞节正在返回一个int,但现在它显示了从ostream到string的无法转换。从(dayCheck == false)开始,整个代码部分出现错误。

(dayCheck == false)

答案 3 :(得分:0)

这里实际上有两个相关的问题。首先,三元运算符的两个分支都需要具有相同的类型(这是运算符结果的类型),因此如果Christmas返回string,则不能有另一个分支返回ostream&的三元运算符。

第二个问题恰恰是Christmas据称返回string。 (我假设return语句实际上在Christmas函数内。)函数的意图不是返回一个string,而是返回一个字符串,并递归地返回其他字符串{{1 }}。因此,唯一有意义的返回类型是coutostream&。但是,如果您进行类似的更改,您会发现void是一个错误,因为您无法向自己发送(cout << Christmas(...))流。

有趣的是看看如何通过<<返回来解决这个问题,尽管它最终看起来与你当前的程序完全不同。以下内容本质上是有缺陷的,因为像ostream&这样的函数应该能够输出到任何流,但我们可以稍后修复它。让我们首先假设函数返回Christmas并且它也输出到同一个ostream&。由于函数返回ostream,我们应该使用它而不是ostream&,所以我们可能会有类似这样的东西:

cout

但是,递归现在已经转过头了。递归调用在之前发生,我们设法将当前行发送到返回的std::ostream& Christmas(int day, int count, bool startStanza) { return (/* We're not finished */) ? Christmas(/* the next line */) << /* This line */; : std::cout; /* FIXME */ } 。所以我们实际上需要从最后开始,然后向前推进。事实证明,这并不困难,我们甚至可以摆脱布尔:

ostream

如果我们要使用std::ostream& Christmas(int day, int count) { return day ? count <= day ? Christmas(day, count + 1) << gift[count] << '\n' : Christmas(day - 1, 1) << "On the " << day << " day of Christmas, " "my true love gave to me:\n" : std::cout; /* FIXME */ } ,我们也可以保持一致。假设我们想通过写下来开始整个事情:

<<

要做到这一点,我们需要两件事:

  1. 一个类对象,用于保存std::cout << Christmas; ,其成员函数与上述类似。

  2. 一个I/O manipulator,它是一个函数,用于构造对象,并调用其成员函数来输出carol。

  3. 以下是它们的外观:

    ostream&

    现场直播:http://coliru.stacked-crooked.com/a/1f801eadf8f5261e