我正在为二次方程编写一个类。我已经得到了.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 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发布于 2011-06-15 20:58:54
头文件显示display()不带参数。您已经编写了一个接受参数的代码,但是没有包含它们的类型:
void display (my_a, my_b, my_c) 从清空这些括号开始,事情应该会变得更好。
其次,display应该是类的成员函数。这就是它访问my_a、my_b和my_c的方式。
void quadraticEquation::display() 第三,hasRealRoots也应该是该类的成员函数,不带任何参数-并且您的代码不应该仅仅查看两个数字是否都为正(这没有任何意义),而应该实际计算b^2-4ac项,看看它是否为正(这意味着等式的根将是实数而不是复数)。
发布于 2011-06-15 21:04:27
显示功能的使用错误(在cpp文件中)。只需使用它作为void display(),因为它不需要params,并且它需要的所有params都已经初始化。错过了一个要点..
将它写为void quadraticEquation::display(),而不是void display()
发布于 2011-06-15 20:59:22
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;
}https://stackoverflow.com/questions/6357956
复制相似问题