编译错误:类型'const char [5]'和'double(*)(double,double,double,double,double)'到二进制'operator +'的操作数无效

时间:2014-09-27 01:46:04

标签: c++ string function

我尝试制作打印功能,其中我合并了一些文本和从函数中获得的数值。编译时收到错误:

错误消息

error: invalid operands of types ‘const char [5]’ and ‘double (*)(double, double, double, double, double)’ to binary ‘operator+’
  wrtResult = ("s = " + sortValues + " kJ/(kg.K)");

源代码

#include <iostream>
#include <cmath>
#include <string>
using namespace std;

void eqOut(double sortValues(double tempC,double xo,double yo,double xone,double yone))
{
    string wrtResult; 
    wrtResult = ("s = " + sortValues + " kJ/(kg.K)"); // This is the line in question
    cout << wrtResult;
}

int main()
{
    double tempC;
    double xo, xone, yo, yone;

    cout << "Enter a temp in C : ";
    cin >> tempC;


    if ((tempC < 150) || (tempC > 500))

    {
        cout << "Enter a value between 150 and 500 next time!" << endl;
    }

    else
    {

        double sortValues(double tempC,double xo,double yo,double xone,double yone);
        {

            double temp150 = 150, temp200 = 200,
                   temp250 = 250, temp300 = 300,
                   temp400 = 400, temp500 = 500;

            double ent150 = 7.2810, ent200 = 7.5081,
                   ent250= 7.7100, ent300 = 7.8941,
                   ent400 = 8.2236, ent500 = 8.5153;

            double x;


            if (tempC == 150)
            {
                cout << "7.2810 kJ/(kg.K)";
            }

            if (tempC > 150 && tempC < 200)
            {
                x = tempC;
                xo = ent150; 
                xone = ent200;
                yo = temp150;
                yone = temp200;
                return (yone+(yone-yo)*((x-xo)/(xone-xo)));
            }

            if (tempC > 200 && tempC < 250)
            {
                x = tempC;
                xo = ent200;
                xone = ent250;
                yo = temp200;
                yone = temp250;
                return (yone+(yone-yo)*((x-xo)/(xone-xo)));
            }

            if ((tempC > 250) && (tempC < 300))
            {
                x = tempC;
                xo = ent250;
                xone = ent300;
                yo = temp250;
                yone = temp300;
                return (yone+(yone-yo)*((x-xo)/(xone-xo)));
            }

            if ((tempC > 300) && (tempC < 400))
            {
                x = tempC;
                xo = ent300;
                xone = ent400;
                yo = temp300;
                yone = temp400;
                return (yone+(yone-yo)*((x-xo)/(xone-xo)));
            }
            if ((tempC > 400) && (tempC < 500))
            {
                x = tempC;
                xo = ent400;
                xone = ent500;
                yo = temp400;
                yone = temp500;
                return (yone+(yone-yo)*((x-xo)/(xone-xo)));
            }
        }
    }

    eqOut;
    return 0;
}

2 个答案:

答案 0 :(得分:1)

在这一行:

wrtResult = ("s = " + sortValues + " kJ/(kg.K)");

sortValues是一个函数指针。它的名称,如果没有括号,则引用函数指针值;它没有调用该函数。

您可能需要以下内容:

wrtResult = ("s = " + sortValues(1.0, 2.0, 3.0, 4.0, 5.0) + " kJ/(kg.K)");
但是,显然有更明智的论据。 (我说&#34;类似于&#34;,但不是那么多,因为你不能添加字符串文字和double.. If you want to append values to strings, use std :: string`而不是C风格的字符串;你可以用C风格的字符串来做,但你必须自己管理内存。)

这解释了错误消息。在main的正文中,您遇到了一些严重的问题。

看起来你正试图在另一个函数定义中定义一个函数:

int main()
{
    ...
    double sortValues(...);
    {
        ...
    }
    ...
}

但是在函数定义中,您不能在参数列表后面加分号。使用分号,您有一个函数声明(对于需要在其他地方定义的函数),后跟由{和{{分隔的无关复合语句1}}。

您需要将}函数的定义移至顶级,超出sortValues的定义。

答案 1 :(得分:0)

// ...
double sortValues(double tempC,double xo,double yo,double xone,double yone);
                                                                        // ^ Semicolon is wrong!
{
// ...

这看起来非常错!函数体内的函数声明,以及后面的无关定义代码。

相关问题