开发者

Having a problem writing a class in C++; undeclared identifiers

I am coding a class for the quadratic equation. I have been given the .h file, and have to write it based on that. I experience problems when I am trying to establish the "display" function, where I am getting undeclared identifier areas (as shown here):

'my_a' : undeclared identifier

'my_b' : undeclared identifier

'my_c' : undeclared identifier

'display' : function-style initializer appears to be a function definition

I would appreciate a little direction in my code. I am including the .h file at the bottom.

#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 r开发者_StackOverflow社区oot1 , 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;
}

And

#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


The header file shows display() taking no parameters. You've coded one that takes parameters, but you haven't included their types:

void display (my_a, my_b, my_c) 

Start by making those brackets empty and things should get a lot better.

Second, display should be a member function of the class. That's how it will get access to my_a, my_b, and my_c.

void quadraticEquation::display() 

Third, hasRealRoots should also be a member function of the class, taking no parameters - and your code should not just see if both numbers are positive (which makes no sense) but actually evaluate the b^2-4ac term and see if it's positive (meaning the roots of the equation will be real rather than complex.)


the use of your display function is wrong(in cpp file). just use it as void display() since it doesnt need params and all the params it needs are already initialised. missed a point..

write it as void quadraticEquation::display() rather than 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜