条件运算符的返回值
我试图使用条件运算符根据条件返回值 true 或 false,但出现错误。这是我的代码,
bool isEmpty()
{
int listSize = Node::size();
listSize > 0 ? return (true) : return (false);
return false;
}
这是错误,
error C2107: illegal index, indirection not allowed
现在我被困在这里。我没明白这个意思。按逻辑我认为这应该是正确的。请指导我。谢谢
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;
}
And here is the error,
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只能将表达式* 作为三元条件的操作数,而不是语句。通常的说法是:
或者甚至更好,
或者甚至更好,
*) 既然您将其标记为 C 和 C++,请知道有一个 两种语言中可接受的表达式之间存在细微差别。
You can only have expressions* as the operands of the ternary conditional, not statements. The usual way to say this is:
or even better,
or even better,
*) Since you tagged this as both C and C++, know that there is a subtle difference between the admissible expressions in the two languages.
三元运算符 (
?:
) 并非设计用于这样的使用。您有语法错误。试试这个:
The ternary operator (
?:
) is not designed to be used like that. You have a syntax error.Try this instead:
除非你有我所缺少的更深层的原因这样做,否则你应该
return (listSize > 0);
。Unless you have a deeper reason for doing this that I am missing, you should just
return (listSize > 0);
.