error help plz

Jul 11, 2011 at 1:23am
getting this error on the lines of the 2 equations, not sure why..?
error C2064: term does not evaluate to a function taking 1 arguments


float quad(istream& ins, ostream& outs)
{
//local data
int a;
int b;
int c;
float qans1;
float qans2;

//get numbers from file
ins >> a;
ins >> b;
ins >> c;

//solve
qans1 = (-b + sqrt((b^2) - (4(a * c)))) / (2 * a);
qans2 = (-b - sqrt((b^2) - (4(a * c)))) / (2 * a);


return 0;
}
Jul 11, 2011 at 1:29am
Could you post the line of code which called this function quad()?

Sounds like 2 parameters are not being passed to the function like it is described in the definition.
Jul 11, 2011 at 1:34am
void selOp(char choice , istream& ins, ostream& outs)
{

switch (toupper(choice))
{
case '1': // Perform a queadratic equation with input.txt
float quad(istream& ins, ostream& outs);
break;

case '2': // Perform a polynomial equation with input.txt
float poly();
break;

case '3': // Exit the program
outs << "Exiting program..." << endl;
break;
}
}
Jul 11, 2011 at 1:55am
You need multiplication operators after the 4's.
Jul 11, 2011 at 2:25am
okay thank you that worked but now i got this >>

error C2668: 'sqrt' : ambiguous call to overloaded function
could be 'long double sqrt(long double)'
or 'float sqrt(float)'
or 'double sqrt(double)'

how do i make my qans2 = (-b - sqrt((b^2) - (4 * (a * c)))) / (2 * a); abide by one of those?
Jul 11, 2011 at 2:49am
You'll need to cast the parameter to sqrt.

If you want it to run the "long double" version of sqrt, do the following:

(-b - sqrt((long double) ((b^2) - (4 * (a * c))))) / (2 * a);

and similarly for the others:

(-b - sqrt((float) ((b^2) - (4 * (a * c))))) / (2 * a);

(-b - sqrt((double) ((b^2) - (4 * (a * c))))) / (2 * a);

The above explicitly tells sqrt how to treat the parameter. Otherwise, it would have to guess (and when there's any ambiguity like this, you receive a compilation error).

Since qans1 and qans2 are floats, I would go with the float version.

Also, the caret "^" does not do exponentiation. It is the bitwise exclusive or operator. Instead, you could use the pow function, or simply do b*b.
Last edited on Jul 11, 2011 at 2:52am
Jul 11, 2011 at 10:08pm
so could you tell me what i put in place of the long double or float or double,
sorry im kinda new..

* and i fixed the '^' right after i posted it so as of now its:

qans2 = (-b - sqrt( pow(b,2)) - (4 * (a * c))) / (2 * a);

but still says i should make it follow long double, float or double, im just not sure what that means
Jul 11, 2011 at 10:59pm
You misplaced a bracket by the way, it should be this:

qans2 = (-b - sqrt( pow(b,2) - (4 * (a * c)))) / (2 * a);

With what you posted above, it would've just taken the square root of b squared. You wanted the square root of "b squared minus 4 a c".

---

The error you're getting means that it doesn't know how to treat the parameter to the "sqrt" function. The parameter, namely, pow(b,2) - (4 * (a * c)) can be treated as type long double OR type float OR type double. The compiler can't handle the ambiguity. It needs you to tell it how to treat the parameter.

Forcing an expression to be a certain type is called type casting.

e.g.

1
2
3
4
5
int main()
{
     float answer = ((float)5)/2;
     return 0;
}


On line 3, the literal 5 is being type cast as a float (it would've been treated as an int otherwise).

In your case, since ans1 and ans2 are both floats, you should type cast the expression "pow(b,2) - (4 * (a * c))" as a float for both calls to sqrt. I have given an example above (and in my previous post) on how to do this.
Last edited on Jul 11, 2011 at 11:00pm
Topic archived. No new replies allowed.