对头文件的未定义引用和使用增变器的问题

时间:2019-04-12 04:51:54

标签: c++ class header mutators

我需要创建一个程序来从计算二次方根的输入文件中打印文本文件。当我编译main.ccp时出现错误 “未定义对QuadraticEquation ::的引用”,然后调用任何函数。我在理解如何使用和检查mutators以及获取头文件中的函数时也遇到了问题。

我是编程的新手,仍然学习类,因此可能是语法问题,或者完全是错误的。

很抱歉粘贴整个内容,但是我不知道是哪个部分

我的标头'QuadraticEquation.h'

#ifndef QUADRATICEQUATIONSOLVER_QUADRATICEQUATION_H
#define QUADRATICEQUATIONSOLVER_QUADRATICEQUATION_H

#endif //QUADRATICEQUATIONSOLVER_QUADRATICEQUATION_H
#include <iostream>
#include <iomanip>
using namespace std;

class QuadraticEquation
{
public:
    /*
    Constructor - store the values of the quadratic equation
    */
    QuadraticEquation(double a, double b, double c);
    QuadraticEquation();
    /*
    Mutators...
    */
    void setA(double a);
    void setB(double b);
    void setC(double c);
    /*
    getSolution1 returns one of the solutions OR
    0 if no solution exists.  getSolution1 returns
    the smaller of the two solutions.
    */
    double getSolution1(double a, double b,double c) {
        return (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
    };
    /*
    getSolution2 returns one of the solutions OR
    0 if no solution exists.  getSolution2 returns
    the larger of the two solutions.
    */
    double getSolution2(double a, double b, double c){
        return (-b + sqrt((b*b)-(4*a*c)))/(2*a);
    };
    /*
    hasSolutions returns true if
    sqrt(b^2-4ac) is positive
    */
    bool hasSolutions(double a, double b,double c) {
            return( (b*b)-(4*a*c) >= 0);
            };

    double getA()
    {
        return a;
    };

    double getB()
    {
        return b;
    };

    double getC()
    {
        return c;
    }

    ;


private:
    double a, b, c;

};

和我的main.cpp(在while语句之上的所有内容均已给出,因此不应该成为问题)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include "QuadraticEquation.h"

using namespace std;

int main(int argc, char* argv[]) {
    double aVal, bVal, cVal;
    string bSign, cSign;
    ifstream inputFile;
    ofstream outputFile;
    string infile = argv[1];
    string outfile = argv[2];

    if (argc != 3) {
        cout << "Incorrect number of parameters." << endl;
        cout << "Parameter count == " << argc << endl;
        cout << "Usage: " << argv[0] << " infile outfile" << endl;
        cout << "Exiting..." << endl << endl;
        return -1;
    }

    // open the input file and the output file
    inputFile.open(infile);
    if (inputFile.fail()) {
        cout << "Error opening input file, " << infile << ". Exiting..." << endl;
        return -1;
    }
    outputFile.open(outfile);
    if (outputFile.fail()) {
        cout << "Error opening output file, " << outfile << ". Exiting..." << endl;
        return -1;
    }
    //
    // read through the input file.  Each line contains 3 numbers.  The a, b, and c
    // values of the quadratic equation.
    //
    while (inputFile >> aVal >> bVal >> cVal) {

            QuadraticEquation quad;
            double x1 = quad.getSolution1(aVal, bVal, cVal);
            double x2 = quad.getSolution2(aVal, bVal, cVal);

            if (bVal >= 0) {
                bSign = "+ ";}
            else bSign = "- ";

            if (cVal >= 0) {
                cSign = "+ ";}
            else cSign = "- ";

            cout << aVal << "x^2  " << bSign << bVal << "x " << cSign << cVal;

        if (quad.hasSolutions(aVal, bVal, cVal)) {
            cout << ": x = " << x1 << " and x = " << x2 << endl << endl;
            outputFile << ": x = " << x1 << " and x = " << x2 << endl << endl;
        }
        else {
            cout << ": there are no real solutions." << endl << endl;
            outputFile << ": there are no real solutions." << endl << endl;
        }

    }
    //check default constructor
    cout << endl << "Checking default constructor..." << endl << endl;

    cout << aVal << "x^2 + " << bVal << "x + " << cVal << endl;

    //check mutators
    cout << endl << "Checking mutators..." << endl << endl;
    QuadraticEquation quad;

    quad.setA(1);
    quad.setB(-2);
    quad.setC(-3);

    cout << "Solution 1 should be -1.00.  Solution 1 is " << quad.getSolution1() << endl;
    cout << "Solution 2 should be 3.00.  Solution 2 is " << quad.getSolution2() << endl;

    return 0;
}

如果需要,我可以发布输入文件以及输出应该是什么样子,我相信我的格式正确。现在,我只需要编译一下即可。

0 个答案:

没有答案