用C ++编写类时遇到问题;未声明的标识符

问题描述

| 我正在为二次方程编码一个类。我已经得到了.h文件,并且必须根据该文件进行编写。当我尝试建立\“ display \”函数时,我遇到未声明的标识符区域(如下所示)时遇到问题: \'my_a \':未声明的标识符 \'my_b \':未声明的标识符 \'my_c \':未声明的标识符 \'display \':函数样式的初始值设定项似乎是函数定义 我希望代码中有一点指导。我在底部包括.h文件。
#include <iostream> // for cout,cin,istream,ostream
#include <cmath> // for floor
#include <string> // for class string
#include \"quad.h\"
using namespace std;


quadraticEquation::quadraticEquation (double initA,double initB,double initC)
{
my_a = initA;
my_b = initB;
my_c = initC;
}

double quadraticEquation:: root1() const
{
double result = 0.0;
result= ((-1* my_b)+(sqrt((my_b*my_b)- (4*my_a*my_c)))/(2*my_a));
return result;
}

double quadraticEquation:: root2() const
{
double result = 0.0;
result= ((-1*my_b)- (sqrt((my_b*my_b)- (4*my_a*my_c)))/(2*my_a));
return result;
}

bool hasRealRoots(double root1,double root2) 
  // post: returns true if an only if b*b-4*a*c >= 0.0,otherwise return false
{
    bool result;
    {
    if (root1 >= 0.0) {
        if (root2 >= 0.0){
        result = true;
        }
    else
    {
        return false;}
}
    }
}

void display (my_a,my_b,my_c)
// post: shows the quadratic equation like -1x^2 + 3x - 9.7
  //       when my_a == -1,my_b = 3,and my_c == -9.7
{   
        if (my_a >= 0)
            cout <<my_a<< \"x^2\"<<;
        else
            cout <<\"-\"<< abs(my_a)<<\"x^2\"<<;
        if(my_b >= 0)
            cout << \" + \" << my_b << \"x\";
        else
            cout << \" - \" << abs(my_b) << \"x\";
        if (my_c >= 0)
            cout <<\" + \"<<my_c<< endl;
        else
            cout << \" - \"<<my_c<< endl;
        return display;
}
#ifndef _QUAD_H
#define _QUAD_H
// file name: quad.h  (the file on disk lists pre- and post-conditions)

class quadraticEquation {
public:
//--constructor (no default constructor for quadraticEquation)
  quadraticEquation(double initA,double initC); 
  // post: initialize coefficients of quadratic equation initA*x*x + initB + c

//--accessors  
  double root1() const;
  // pre: there is at least one real root: b*b-4*a*c >= 0.0 
  // post: returns one real root as  (-b+sqrt(b*b-4*a*c)) / (2*a)

  double root2() const;
  // pre: there is at least one real root: b*b-4*a*c >= 0.0 
  // post: returns one real root as  (-b-sqrt(b*b-4*a*c)) / (2*a)

  bool hasRealRoots() const;
  // post: returns true if an only if b*b-4*a*c >= 0.0,otherwise return false

  void display() const;
  // post: shows the quadratic equation like -1x^2 + 3x - 9.7
  //       when my_a == -1,and my_c == -9.7

private:
  double my_a,my_c; // the three coefficients of the quadratic equation
};

#endif
    

解决方法

        头文件显示display()不带任何参数。您已经编码了一个带参数的参数,但尚未包括它们的类型:
void display (my_a,my_b,my_c) 
首先将这些括号留空,然后情况会好得多。 其次,显示应该是该类的成员函数。这样便可以访问my_a,my_b和my_c。
void quadraticEquation::display() 
第三,hasRealRoots也应该是该类的成员函数,不带任何参数-您的代码不仅应该看两个数字是否都是正数(这没有任何意义),还应该实际评估b ^ 2-4ac项并查看是否的正数(表示等式的根是真实的,而不是复杂的。)     ,        您的显示功能使用不正确(在cpp文件中)。只需将其用作 无效的display() 因为它不需要参数,并且它需要的所有参数都已经初始化。 遗漏了一点。 将其写为void quadraticEquation :: display()而非void display()     ,        
void quadraticEquation::display (double my_a,double my_b,double my_c)
// post: shows the quadratic equation like -1x^2 + 3x - 9.7
  //       when my_a == -1,my_b = 3,and my_c == -9.7
{   
        if (my_a >= 0)
            cout <<my_a<< \"x^2\"<<;
        else
            cout <<\"-\"<< abs(my_a)<<\"x^2\"<<;
        if(my_b >= 0)
            cout << \" + \" << my_b << \"x\";
        else
            cout << \" - \" << abs(my_b) << \"x\";
        if (my_c >= 0)
            cout <<\" + \"<<my_c<< endl;
        else
            cout << \" - \"<<my_c<< endl;
        return display;
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...