没有可行的重载'='c ++

时间:2018-02-14 17:08:57

标签: c++

我收到错误消息“没有可行的重载'='”。 这是我现在拥有的代码

#include <iostream>
#include <cmath>
using namespace std;
int main() {
    auto n=0;
    int p=0;
    cout << "enter number n:"<< endl;
    cin >> n ;
    cout << p=pow(2,n)*n! << endl; //this is where I get the error message
    cout << "the product is:" << endl;
    cout << p << endl;
    return 0;
}

谁能告诉我我的错误?

1 个答案:

答案 0 :(得分:8)

根据C++ Operator Precedence,运营商<<优先于运营商=

这意味着

cout << p=pow(2,n)*n! << endl;

读作

(cout << p)=(pow(2,n)*n! << endl);

没有任何意义。使用括号保护您的作业:

cout << (p=pow(2,n)*n!) << endl;

或者更好的是,将它分成两个语句:

p=pow(2,n)*factorial(n); // n! does not exist in C++.
cout << p << endl;
相关问题