非常基本的 C++ 程序问题 - 二进制表达式的无效操作数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11620437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:21:35  来源:igfitidea点击:

Very basic C++ program issue - Invalid operands to binary expression

c++

提问by Danny

I just started teaching myself C++ on the Mac, and I have run into some issues.

我刚开始在 Mac 上自学 C++,但遇到了一些问题。

I have written some code that allows the user to enter a number and when they hit enter, the number will be returned to the user.

我编写了一些代码,允许用户输入一个数字,当他们按下回车键时,该数字将返回给用户。

Xcode will absolutely not have it though. Every time I try to run my code, it says that there is an issue with the cin>> thisisanumber;code.

Xcode 绝对不会有它。每次我尝试运行我的代码时,它都会说代码有问题cin>> thisisanumber;

The error comes up and says

错误出现并说

Invalid operands to binary expression.Error is on line 10.

Invalid operands to binary expression.错误在第 10 行。

What am I doing wrong?

我究竟做错了什么?

#include <iostream>

using namespace std;

int main()
{
   int thisisanumber();

   cout << "Please enter a number: ";
   cin  >> thisisanumber;
   cin.ignore();
   cout << "You entered"<< thisisanumber <<"\n";
   cin.get();
}

回答by chris

You've fallen victim to the most vexing parse, which means thisisanumberis being treated as a function. Take out the parentheses and you should be fine:

您已经成为最令人烦恼的 parse 的受害者,这意味着thisisanumber被视为一个函数。去掉括号,你应该没问题:

int thisisanumber;

Also consider making it a bit more readable, such as thisIsANumber. If you ever need to know it, thisIsANumberuses the camel-casenaming convention.

还可以考虑使其更具可读性,例如thisIsANumber. 如果您需要了解它,请thisIsANumber使用驼峰命名约定。

回答by Péter T?r?k

Declare your variable without brackets, like

声明没有括号的变量,例如

int thisisanumber;

With brackets, it is interpreted as a function, and a function can't be passed as a parameter to the >>operator.

带括号,它被解释为一个函数,并且一个函数不能作为参数传递给>>操作符。

回答by Grizzly

Your problem is the so called most vexing parse. Basically everything, which could be parsed as a function declaration will be parsed as such. Therefore the compiler will interpret int thisisanumber();as a declaration of a function thisisanumbertaking zero arguments and returning an int. If you consider this behaviour the problems with cin>>thisisanumber;should be somewhat selfevident.

你的问题是所谓的最烦人的 parse。基本上所有可以解析为函数声明的东西都将被解析。因此,编译器将解释int thisisanumber();为一个函数声明,该函数thisisanumber采用零参数并返回一个int. 如果您考虑这种行为,问题cin>>thisisanumber;应该是不言而喻的。

If you remove the parantheses, changing the variable declaration to int thisisanumber;, your program should behave like you'd expect it to with thisisanumberbeing a variable of type int.

如果您删除括号,将变量声明更改为int thisisanumber;,您的程序应该像您期望的那样thisisanumber作为类型的变量int

You might however reconsider your naming conventions, thisisanumberisn't exactly readable. I would suggest going with this_is_a_number, thisIsANumberor ThisIsANumber.

然而,您可能会重新考虑您的命名约定,thisisanumber这并不完全可读。我建议使用this_is_a_number,thisIsANumberThisIsANumber

回答by kiros hailay

int thisIsANumber;

try making it variable declaration because what you wrote has been interpreted as function.

尝试使其成为变量声明,因为您编写的内容已被解释为函数。