C ++代码无法正常工作

时间:2014-12-22 23:36:02

标签: 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;
    }
}

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

1 个答案:

答案 0 :(得分:8)

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

将您的代码更改为:

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

Protip:如果用const注释函数的参数,编译器会给你一个错误的表达式:

int thisIsHowYouIfLikeABoss2( const int x, const int y )

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

相关问题