0 c++
我是StackExchange的新手,但我有一个简单的类,当我运行它时似乎没有返回正确的结果.这是代码:
#include <iostream>
using namespace std;
int thisIsHowYouIfLikeABoss2(int, int);
int main()
{
cout << "One." << endl;
thisIsHowYouIfLikeABoss2(9, 9);
cout << "Two." << endl;
thisIsHowYouIfLikeABoss2(4, 9);
return 0;
}
int thisIsHowYouIfLikeABoss2 (int x, int y)
{
cout << "Welcome to the thisIsHowYouIfLikeABoss(), where I calculate if x = y easily." << endl;
if (x = y)
{
cout << "X is Y." << endl;
}
if (x != y)
{
cout << "X is not Y" << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我的编译器是Ubuntu的GNU C++编译器,如果有人想知道的话.
=是赋值运算符,而不是关系相等运算符==.
将您的代码更改为:
if (x == y)
{
cout << "X is Y." << endl;
}
Run Code Online (Sandbox Code Playgroud)
Protip:如果你注释你的函数的参数const然后编译器会给你一个错误的表达式:
int thisIsHowYouIfLikeABoss2( const int x, const int y )
Run Code Online (Sandbox Code Playgroud)
(与C#和Java不同,const在C++中并不意味着该值是编译时固定值或文字,因此您可以使用const变量).