链接类头,类实现和驱动程序问题

时间:2015-02-19 04:34:11

标签: c++ class driver header-files

如果没有包含quadratic.cpp文件的驱动程序,我似乎无法使用它。我把它设置为Dev C ++中的一个项目,但是继续得到错误,说明对驱动程序中调用的所有函数的未定义引用。

quadratic.h

#include <iostream>
using namespace std;

class Quadratic{
    private:
        float a,b,c;

    public:
        //Quadratic();
        Quadratic();
        Quadratic(float a,float b,float c);
        float x;
        float get_a();
        float get_b();
        float get_c();
        float evaluate(float);
        void set();
        int numRoots(Quadratic &qTest);
        float roots1(int numRoots, Quadratic &qRoots);
        float roots2(int numRoots, Quadratic &qRoots);

};

Quadratic operator+(const Quadratic &q1,const Quadratic &q2);
Quadratic operator*(double r, const Quadratic& q);

quadratic.cpp

#include<iostream>
#include "quadratic.h"
#include <cmath>        // std::sqrt(double)
using namespace std;






//Default Constructor
Quadratic::Quadratic():a(0),b(0),c(0){}


//Had to add this to get the overloaded + operator to work
//(mainly because of the way I had it return the new instance)
Quadratic::Quadratic(float aSum, float bSum, float cSum)
{
        this->a = aSum;
        this->b = bSum;
        this->c = cSum;

}

float Quadratic::get_a()
    {
        return a;
    }
float Quadratic::get_b()
    {
        return b;
    }
float Quadratic::get_c()
    {
        return c;
    }       

float Quadratic::evaluate(const float x)
    {
    return ((a*(x*x))+(b*x)+c);

    }
void Quadratic::set()
    {
        float aNew=0.0,bNew=0.0,cNew=0.0;

        cout<<"Enter a new a: ";
        cin>>aNew;
        cout<<"Enter a new b: ";
        cin>>bNew;
        cout<<"Enter a new c: ";
        cin>>cNew;

        this->b = bNew;
        this->c = cNew;
        this->a = aNew;
    }
int Quadratic::numRoots(Quadratic &qTest)
{
    float a= qTest.get_a();
    float b= qTest.get_b();
    float c= qTest.get_c();


    int numRoots=0;

    if ((a==0)&&(b==0)&&(c==0))
        {
            //every value of x = real root, so infinity
            numRoots=3;
        }
    else if((a==0)&&(b==0)&&(c!=0))
        {
            numRoots=0;
        }
    else if((a==0)&&(b!=0))
        {
            numRoots=1;
            //root is x= -c/b
        }
    else if((a!=0)&&( (b*b)<(4*a*c) ))
        {
            numRoots=0;
        }   
    else if((a!=0)&&( (b*b)==(4*a*c) ))
        {
            numRoots=1;
            //root  is x= -b/2a
        }
    else if((a!=0)&&( (b*b)>(4*a*c) ))
        {
            numRoots=2;
            //root is  x= +/- quadratic formula
        }

    return numRoots;
}

float Quadratic::roots1(int numRoots, Quadratic &qRoots)
    {
    float root1=0.0;
    float rootPlus=0.0;
    float rootMinus=0.0;




        if (numRoots==1)
            {
                if((a==0)&&(b!=0))
                    {
                    root1 = (((0-c)/b));// + ((0-c)%b));
                    }
                if((a!=0)&&( (b*b)==(4*a*c) ))
                    {           
                    root1 = (((0-b)/(2*a)));// + ((0-b)%(2*a)));
                    }   
            }   

        else if (numRoots == 2)
            {
                rootMinus= (((0-b)-sqrt((b*b)-(4*a*c)))/(2*a)); 
                rootPlus= (((0-b)+sqrt((b*b)-(4*a*c)))/(2*a));

                if (rootMinus > rootPlus) root1=rootPlus;
                else root1=rootMinus;
            }
        else if (numRoots == 3) root1=0;

return root1;
}

float Quadratic::roots2(int numRoots, Quadratic &qRoots)
    {
    float root2=0.0;
    float rootPlus=0.0;
    float rootMinus=0.0;




        if (numRoots==1)
            {
                if((a==0)&&(b!=0))
                    {
                    root2 = (((0-c)/b));// + ((0-c)%b));
                    }
                if((a!=0)&&( (b*b)==(4*a*c) ))
                    {           
                    root2 = (((0-b)/(2*a)));// + ((0-b)%(2*a)));
                    }   
            }   

        else if (numRoots == 2)
            {
                rootMinus= (((0-b)-sqrt((b*b)-(4*a*c)))/(2*a)); 
                rootPlus= (((0-b)+sqrt((b*b)-(4*a*c)))/(2*a));

                if (rootMinus < rootPlus) root2=rootPlus;
                else root2=rootMinus;
            }
        else if (numRoots == 3) root2=0;

return root2;


}


Quadratic operator+(Quadratic &q1,Quadratic &q2)
    {

        float tempa,tempb,tempc=0.0;
        tempa= q1.get_a() + q2.get_a();
        tempb= q1.get_b() + q2.get_b();
        tempc= q1.get_c() + q2.get_c();

        Quadratic temp(tempa, tempb,tempc);
        return temp;


//      return Quadratic( q1.get_a() + q2.get_a(),
//                        q1.get_b() + q2.get_b(),
//                        q1.get_c() + q2.get_c());
    }

Quadratic operator*(double r,  Quadratic& q)
    {
        return Quadratic( q.get_a() * r,
                          q.get_b() * r,
                          q.get_c() * r);
    }

的main.cpp

#include<iostream>
//#include "quadratic.cpp""
#include "quadratic.h"
using namespace std;




int main()
{
    //Default constructors called, all 3 coefficients set to 0
    Quadratic q1;
    Quadratic q2;

    cout<<"Default constructor called\n"
        <<"q1 a = "<<q1.get_a()<<"\n"
        <<"q1 b = "<<q1.get_b()<<"\n"
        <<"q1 c = "<<q1.get_c()<<"\n"
        <<"q2 a = "<<q2.get_a()<<"\n"
        <<"q2 b = "<<q2.get_b()<<"\n"
        <<"q2 c = "<<q2.get_c()<<"\n";

    //Call the set function to set all 3 coefficents of existing
    //quadratic expression to new values
    q1.set();
    q2.set();

    cout<<"\nSet function called. New values:\n"
        <<"q1 a = "<<q1.get_a()<<"\n"
        <<"q1 b = "<<q1.get_b()<<"\n"
        <<"q1 c = "<<q1.get_c()<<"\n"
        <<"q2 a = "<<q2.get_a()<<"\n"
        <<"q2 b = "<<q2.get_b()<<"\n"
        <<"q2 c = "<<q2.get_c()<<"\n";


    //calls function to evaluate the quadratic expression for a given calue of x
    cout<<"\nEnter x: ";
    float x=0.0;
    cin>>x;

    cout<<"for q1, ax^2 + bx +c = "<<q1.evaluate(x)
        <<"\nfor q2, ax^2 + bx +c = "<<q2.evaluate(x);


    //add q1+q2 with overloaded + operator
    Quadratic quad_sum = q1 + q2;

    cout<<"\n\nAdding q1+_q2\n"
        <<"\nq1_a + q2_a= "<<quad_sum.get_a()
        <<"\nq1_b + q2_b= "<<quad_sum.get_b()<<" "
        <<"\nq1_c + q2_c= "<<quad_sum.get_c();



    //multiply the coefficients by r using overloaded * operator
    cout<<"\n\nEnter a number to multiply the coefficeints by: ";
    double r=0.0;
    cin>>r;
    Quadratic quad_prod=r*quad_sum;

    cout<<"\nMultiplying q1 by r results in "
        <<"\nnew a = "<<quad_prod.get_a()
        <<"\nnew b = "<<quad_prod.get_b()
        <<"\nnew c = "<<quad_prod.get_c();  


    //calculate number of and display roots
    int numroots=q1.numRoots(q1);
    cout<<"\n\nNumber of Roots for q1 = "<<numroots;
    if (numroots != 0) cout<<"\nThey are: "<<q1.roots1(numroots, q1)<<" and "<<q1.roots2(numroots, q1);

}

已添加

#ifndef QUAD_H_ 
#define QUAD_H_

#endif

到头文件,但仍然为驱动程序中的每个函数调用获取“未定义的引用”。

0 个答案:

没有答案