Max
Max

Reputation: 3

Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>' and 'void')

I'm trying to write a function to solve a quadratic equation, and I get this error on line 26:

Invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>' and 'void')

But I don't quite understand why the compiler swears and what it wants from me. I connected the library <ostream>, but it didn't work. Please help me figure it out! Thanks.

This is the code:

#include <iostream>
#include <math.h>
#include <ostream>

using namespace std;

void Kva(float a,float b, float c)
{
    float Diskr, x1, x2;

    Diskr = (b*b) - (4*a*c);

    if (Diskr >= 0)
    {x1 = ((-b) - sqrt(Diskr))/(2*a);
    x2 = ((-b) + sqrt(Diskr))/(2*a);
    cout << "x1 =" << " " << x1 << ";" << " " << "x2 =" << " " << x2 << endl;}
    else {cout << "Diskriminant < 0" << endl;}

    //return x1;
}

int main()
{
    float a, b, c;
    cin >> a >> b >> c;
    cout << Kva(a, b, c) << endl;
    return 0;
}

Upvotes: 0

Views: 3074

Answers (1)

Rohan Bari
Rohan Bari

Reputation: 7726

Look at the prototype of your function:

void Kva(float a, float b, float c)

You are telling the compiler to pass nothing. You may want to ask the function to return the float type. Uncomment the return statement.

For x1 in Kva(), remember to initialize it so that it guarantees to return some value to the function (at least zero) to avoid any unwanted results.

Upvotes: 1

Related Questions