-1

I'm attempting to make a print function where I combine a few pieces of text and a numerical value obtained from a function. I receive the error when compiling:

Error Message

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

Source Code

#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;
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
Buttons
  • 1
  • 3

2 Answers2

1

On this line:

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

sortValues is a function pointer. Its name, when not followed by parentheses, refers to the function pointer value; it doesn't call the function.

You probably want something like:

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

though obviously with more sensible arguments. (I say "something like", but not too much like that, since you can't add a string literal and a double.. If you want to append values to strings, usestd::string` rather than C-style strings; you can do it with C-style strings, but you have to manage the memory yourself.)

That explains the error message. In the body of main, you've got some serious problems.

It looks like you're trying to define a function inside another function definition:

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

But in a function definition, you can't have a semicolon after the parameter list. With the semicolon, you have a function declaration (for a function that needs to be defined elsewhere) followed by an unrelated compound statement delimited by { and }.

You need to move the definition of your sortValues function to the top level, outside the definition of main.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 2
    Even then that's still broken, since you can't add a C-style string and a `double`. – T.C. Sep 27 '14 at 01:53
  • As @T.C. said, you should probably use `std::to_string`. – ikh Sep 27 '14 at 01:55
  • So I can do else and just call the function that is defined elsewhere? In regard to the C-style string 'double' I presume you are referring to the + functionpointer + ? – Buttons Sep 27 '14 at 02:00
  • @Buttons _"So I can do else and just call the function that is defined elsewhere?"_ That's the usual concept, yes. Even if you would fully define your function at that place, you would need to call it afterwards. – πάντα ῥεῖ Sep 27 '14 at 02:11
  • Awesome, thanks for the wisdom stackcrew! I'll rearrange, simplify and give it another go. – Buttons Sep 27 '14 at 02:13
0
// ...
double sortValues(double tempC,double xo,double yo,double xone,double yone);
                                                                        // ^ Semicolon is wrong!
{
// ...

This looks very wrong! Function declaration within a function body, and unrelated definition code following.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190