2014-09-30 20 views
-2
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)。有没有一种方法来演绎这个,或者有另外一种方法我需要格式化它来保持它的嵌套? (对于这个特定的代码,我实际上关注于三元运算符的嵌套)三元运算符C++的ostream为String不兼容的操作数

+2

你可能是指'dayCheck == FALSE'(我认为这是更好的写作'dayCheck',除非你按照“使用枚举而不是布尔值来指示使用哪种替代方法”的风格建议。这是您的问题的一个侧面问题。 – rici 2014-09-30 05:14:08

+0

感谢提示@rici – TeddyCode 2014-09-30 06:00:48

回答

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); 
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 
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的串一个没有可行的转换。错误现在出现在从(dayCheck == false)开始的整个代码段中。

(dayCheck == false) 
+0

这个函数的返回类型是什么?如果它是一个字符串,那么是的,这是行不通的。你会想使用'std :: stringstream'而不是'std :: cout',并返回'stringstream.string()'。 – kmac 2014-10-01 02:29:53

0

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

第二个问题恰恰就是Christmas涉嫌返回string。 (我假设return语句实际上在Christmas函数中。)函数的目的不是返回string,而是返回字符串,并且递归地返回其他字符串到cout。所以唯一有意义的返回类型是ostream&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 */ 
} 

如果我们要使用<<,我们不妨是一致的。假设我们想通过写作来启动整个事情关闭:

std::cout << Christmas; 

要做到这一点,我们需要两样东西:

  1. 一个类的对象,持有ostream&,它具有类似的成员函数到上面。

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

这里的一切的样子:

class Singer { 
    friend std::ostream& Christmas(std::ostream& out); 
    Singer(std::ostream& out) : out(out) {} 
    std::ostream& sing(int day, int count) { 
     return day ? count <= day ? sing(day, count + 1) << (count == 1 && day > 1 ? "and " : "") 
                 << gift[count - 1] << '\n' 
           : sing(day - 1, 1) << "\nOn the " 
                << day 
                << "th day of Christmas, " 
                 "my true love gave to me:\n" 
       : out; 
    } 
    private: 
    std::ostream& out; 
    static const char *gift[12]; 
}; 

std::ostream& Christmas(std::ostream& out) { return Singer(out).sing(12,1); } 

亲身体验:http://coliru.stacked-crooked.com/a/1f801eadf8f5261e