在条件运算符中返回值 [英] Returning value in conditional operator

查看:177
本文介绍了在条件运算符中返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用条件运算符根据条件返回值true或false,但出现错误。这是我的代码,

I was trying to return value true or false depending upon the condition by using a conditional operator but I got an error. Here is my code,

bool isEmpty()
{
    int listSize = Node::size();
    listSize > 0 ? return (true) : return (false);
    return false;
}

这是错误,

error C2107: illegal index, indirection not allowed



<现在我被困在这里。我不明白这一点。从逻辑上讲,我认为应该是正确的。请指导我。谢谢

Now I am stuck here. I don't get the point.Logically I think it should be correct. Please guide me about it . Thanks

推荐答案

您只能使用表达式*作为三元条件运算符,而不是语句。通常的说法是:

You can only have expressions* as the operands of the ternary conditional, not statements. The usual way to say this is:

return listSize > 0 ? true : false;

甚至更好,

return listSize > 0;

甚至更好,

bool isEmpty() { return Node::size() > 0; }


*)由于您同时将其标记为C和C ++,因此请知道存在细微差异在两种语言的允许表达之间。

*) Since you tagged this as both C and C++, know that there is a subtle difference between the admissible expressions in the two languages.

这篇关于在条件运算符中返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆