简体   繁体   English

C++ 错误:“被调用的对象类型‘int’不是函数或函数指针”

[英]C++ Err: 'Called object type 'int' is not a function or function pointer'

I am attempting to write a quadratic equation calculator in C++, and I keep running into this error.我正在尝试用 C++ 编写一个二次方程计算器,但我一直遇到这个错误。 See if you can figure out why: (the error occurs on the 'g =' line)看看你是否能找出原因:(错误发生在 'g =' 行)

#include <cmath>
#include <iostream>
using namespace std;
int main(){
    int a,b,c,d,e,f,g;
    cout<<"Enter 'A' value:";
    cin>>a;
    cout<<"Enter'B' value:";
    cin>>b;
    cout<<"Enter 'C' value:";
    cin>>c;
    e = ((b * b) - (4 * (a*c)));
    d = sqrt(e);
    f = ((-b) + d) / (2 * a);
    g = ((-b) - d) / (2 * a);
    if(d > 0){
        cout<<"One solution is: "<<f<<endl;
        cout<<"The other solution is: "<< g <<endl;
    }
    else if(d == 0){
        cout<<"Your 1 solution is: "<< f <<endl;
    }
    else{
        cout<<"No real solutions!"<<endl;
    }
}

Any help is appreciated!任何帮助表示赞赏!

The square root function will return floating point values, so you should use a floating point variable like double to store the result.平方根函数将返回浮点值,因此您应该使用像 double 这样的浮点变量来存储结果。

#include <cmath>
#include <iostream>
using namespace std;
int main(){
    int a,b,c,e;
    double d,f,g;
    cout<<"Enter 'A' value:";
    cin>>a;
    cout<<"Enter'B' value:";
    cin>>b;
    cout<<"Enter 'C' value:";
    cin>>c;
    e = ((b * b) - (4 * (a*c)));
    d = sqrt(e);
    f = ((-b) + d) / (2 * a);
    g = ((-b) - d) / (2 * a);
    if(d > 0){
        cout<<"One solution is: "<<f<<endl;
        cout<<"The other solution is: "<< g <<endl;
    }
    else if(d == 0){
        cout<<"Your 1 solution is: "<< f <<endl;
    }
    else{
        cout<<"No real solutions!"<<endl;
    }
}

The problem is that the square root function returns a floating point decimal, and you are trying to combine it with an integer.问题是平方根函数返回一个浮点小数,而您正试图将它与一个整数组合。 For example, 1 - 2.0.例如,1 - 2.0。 Both of the values have to be floating point decimals, so you should change "int" to "double" on line 5.这两个值都必须是浮点小数,因此您应该在第 5 行将“int”更改为“double”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CPP 错误:“被调用的对象类型 &#39;int (hashTable::*)(int, int)&#39; 不是函数或函数指针” - CPP err: "called object type 'int (hashTable::*)(int, int)' is not a function or function pointer" 称为 object 类型 'int' 不是 function 或 function 指针 - Called object type 'int' is not a function or function pointer 调用对象类型'void(B :: *)(int)'不是函数或函数指针 - called object type 'void (B::*)(int)' is not a function or function pointer 称为 object 类型 'int' 不是 function 或 function 指针 + 枚举 - called object type 'int' is not a function or function pointer + enums 错误:调用 object 类型 'int' 不是 function 或 function 指针 - error: called object type 'int' is not a function or function pointer prog.c:42:27: 错误:被调用的对象类型“int”不是函数或函数指针 - prog.c:42:27: error: called object type 'int' is not a function or function pointer 调用对象'char *'类型不是函数或函数指针 - called object 'char* 'type is not a function or function pointer 称为“自动”的对象类型不是函数或函数指针 - Called object type 'auto' is not a function or function pointer 被调用的对象类型 bool 不是函数或函数指针 - called object type bool is not a function or function pointer 指向C ++中的函数对象 - pointer to function object in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM