【问题标题】:Compiling error: invalid operands of types ‘const char [5]’ and ‘double (*)(double, double, double, double, double)’ to binary ‘operator+’编译错误:‘const char [5]’和‘double (*)(double, double, double, double, double)’类型的无效操作数到二进制‘operator+’
【发布时间】:2014-09-27 01:46:03
【问题描述】:

我正在尝试创建一个打印函数,其中我将几段文本和从函数中获得的数值结合起来。编译时收到错误:

错误信息

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;
}

【问题讨论】:

  • 我不知道从哪里开始
  • 你需要get a good book
  • 很糟糕吧?哎呀。我们昨天刚刚介绍了函数,所以我确定我完全误解了它们。也许我应该回到这里的绘图板并一起删除这个声明。
  • @Buttons “很糟糕吧?” 很抱歉,但是:是的!真是太糟糕了……
  • 太好了,谢谢你>_

标签: c++ string function


【解决方案1】:

在这一行:

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

sortValues 是一个函数指针。它的名字,如果后面没有括号,指的是函数指针值;它不调用函数。

你可能想要这样的东西:

wrtResult = ("s = " + sortValues(1.0, 2.0, 3.0, 4.0, 5.0) + " kJ/(kg.K)");

虽然显然有更明智的论点。 (我说“类似”,但不是太像,因为您不能添加字符串文字和 double.. If you want to append values to strings, usestd::string` 而不是 C 样式的字符串;您可以使用 C 样式的字符串,但你必须自己管理内存。)

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

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

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

但是在函数定义中,参数列表后面不能有分号。使用分号,您有一个函数 declaration(对于需要在别处定义的函数),后跟一个由{} 分隔的不相关复合语句。

您需要将sortValues 函数的定义移至main 定义之外的顶层。

【讨论】:

  • 即便如此,它仍然被破坏,因为你不能添加 C 风格的字符串和 double
  • 作为@T.C.说,你应该使用std::to_string
  • 所以我可以做其他事情,只调用在别处定义的函数?关于 C 风格的字符串 'double' 我想你指的是 + 函数指针 + ?
  • @Buttons “所以我可以做其他事情,只调用其他地方定义的函数?” 这是通常的概念,是的。即使你会在那个地方完全定义你的函数,你也需要在之后调用它。
  • 太棒了,感谢智慧的堆栈人员!我会重新安排、简化并再试一次。
【解决方案2】:
// ...
double sortValues(double tempC,double xo,double yo,double xone,double yone);
                                                                        // ^ Semicolon is wrong!
{
// ...

这看起来很不对劲!函数体内的函数声明,后面是不相关的定义代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2018-10-25
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多