gpt4 book ai didi

C++ 使用默认参数重载

转载 作者:行者123 更新时间:2023-11-28 00:03:46 25 4
gpt4 key购买 nike

我有一个小问题,如何初始化函数中的默认参数?

    #include <iostream>
#include <cmath>

using namespace std;
float area(float a, float b, float c);
float area(float a, float b=a, float c =a);


int main() {

cout << area(10) << endl;
return 0;
}

float area(float a, float b, float c){
return a*b*c
}

我收到错误消息,我该如何正确执行?

最佳答案

你将不得不使用重载而不是默认参数:

#include <iostream>
#include <cmath>

using namespace std;
float area(float a, float b, float c);
float area(float a);

int main() {

cout << area(10) << endl;
return 0;
}

float area(float a, float b, float c){
return a*b*c;
}
float area(float a){
return area(a,a,a);
}

关于C++ 使用默认参数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36960593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com