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

时间:2011-06-15 12:54:15

标签: c++

我正在为二次方程编写一个类。我已经获得了.h文件,并且必须根据它编写它。当我尝试建立“显示”功能时,我遇到了问题,我得到了未声明的标识符区域(如下所示):

'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 initB, 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, my_b = 3, and my_c == -9.7

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

#endif

3 个答案:

答案 0 :(得分:3)

头文件显示display()不带参数。您编写了一个带参数的编码,但您没有包含其类型:

void display (my_a, my_b, my_c) 

首先让这些括号变空,事情应该好多了。

其次,显示应该是类的成员函数。这就是它将如何访问my_a,my_b和my_c。

void quadraticEquation::display() 

第三,hasRealRoots也应该是类的成员函数,不带参数 - 你的代码不应只看两个数字是否为正数(这没有意义),而是实际评估b ^ 2-4ac项并查看如果它是正的(意味着等式的根将是真实的而不是复杂的。)

答案 1 :(得分:1)

使用你的显示功能是错误的(在cpp文件中)。只是用它作为 void display() 因为它不需要参数,所需的所有参数都已初始化。 错过了一点..

将其写为void quadraticEquation :: display()而不是void display()

答案 2 :(得分:0)

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;
}