【问题标题】:How do you add doubles in a function with c++?如何使用 C++ 在函数中添加双精度数?
【发布时间】:2017-02-25 06:03:24
【问题描述】:

基本上我在 c++ 中设置了双打,但由于某种原因它给了我这个错误

"..\project2_name.cpp:56:9: error: assignment of function 'double total(double, double, double)'".

double appleprice=0;
double pearprice=0;
double tomatoprice=0;
double completetotal=0;


double total(double appleprice, double pearprice, double tomatoprice)
{
    total = appleprice + pearprice + tomatoprice;
        return total;
}

我有一个开关盒,它可以从一个除了总数之外的菜单中调用它: 案例“3”: cout

    case '4':
        cout<< "Your Full order" << endl;
        completetotal = total(appleprice, pearprice, tomatoprice);
        cout << "You have on order " << apple << " apples. " << appleprice << " price."<< endl;
        cout << "You have on order " << pear << " pears. " << pearprice << " price."<< endl;
        cout << "You have on order " << tomato << " tomatos. " << tomatoprice << " price."<< endl;
        cout << "You have a total of " << completetotal << endl;
        break;

【问题讨论】:

  • total 是函数的名称。您不能分配给该功能。改为创建一个临时变量。
  • 我明白了……呃。 (打我的头)
  • 某些语言,如 Pascal,允许将返回值分配给函数的名称,而无需为其声明变量。 C/C++ 不能那样工作。

标签: c++


【解决方案1】:

total 是函数的名称 - 你不能使用它。使用临时变量或将代码更改为

double total(double appleprice, double pearprice, double tomatoprice)
{
    return appleprice + pearprice + tomatoprice;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    相关资源
    最近更新 更多