0

当我运行此代码时,我收到以下错误:

一元'*'的无效类型参数(有'int')

我怎样才能清除这个?

#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

void multiplyFive (int &x, int y, int *z){
    int d = 5 * x;
    int e = 5 * y;
    int f = 5 * *z;
    cout << d << " " << e << " " << f << endl;
}

int main() {
    int a = 2;
    int *b = &a;
    int &c = *b;

    multiplyFive (a, b, *c);
    
    return EXIT_SUCCESS;
}
4

1 回答 1

1

Clangs 错误消息更有帮助:

<source>:19:21: error: indirection requires pointer operand ('int' invalid)
multiplyFive (a, b, *c);
                    ^~

c是对int非指针的引用,您不能通过*.

似乎您将 2nd 和 3rd 参数混为一谈multiplyFive,因为您将收到的下一个错误b是 aint*multiplyFive需要 aint代替。

于 2021-02-11T17:15:52.867 回答