C++ 代码不起作用

C++ Code not working

本文关键字:不起作用 代码 C++      更新时间:2023-10-16

我是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;
    }
}

我的编译器是GNU C++Ubuntu编译器,如果有人想知道的话。

>=是赋值运算符,而不是关系相等运算符,后者==

将您的代码更改为以下内容:

if (x == y)
{
    cout << "X is Y." << endl;
}

专业提示:如果您使用 const 注释函数的参数,那么编译器会给您一个表达式错误:

int thisIsHowYouIfLikeABoss2( const int x, const int y )

(与 C# 和 Java 不同,C++ 中的const并不意味着该值是编译时固定值或文本值,因此您可以将const与变量一起使用)。