I have to make a quadratic equation calculator out of classes and use structures with arrays. I'll post the code I have below and the errors I'm getting after that. Please help!
file: solver.h
Code:
// file solver.h
// Specification of a class providing service of solving
// quadratic equations of the form a*b^2 + b*x + c = 0
#ifndef COUNTER_H
#define COUNTER_H
class solver {
public:
struct coeffStruct {
double coeffs[3];
bool coeffsFlag; // is a equal zero?
};
struct rootStruct {
double roots[2];
bool rootFlag; // are roots existing?
};
public:
// Constructor
solver();
// Function to read coefficients a, b, c, from the keyboard
// and return them into a 3-element array (plus boolean,
// whether a is equal zero or not)
coeffStruct readCoeffs();
// Function to calculate roots and return them into
// a 2-element array (plus Boolean, whether they exist)
rootStruct calcRoots(double);
// Function to display values of the roots or an error message,
// if the roots do not exist
void displayRoots(double);
private:
// Function to calculate the discriminant
double discr(double);
};
#endif // COUNTER_H
file: solver.cpp
Code:
#include "solver.h"
#include <iostream>
#include <cmath>
using namespace std;
////////////////////////////////////////////////////////////////////////
// Default constructor
solver::solver() {
}
////////////////////////////////////////////////////////////////////////
solver::coeffStruct solver::readCoeffs() {
coeffStruct coeffs;
cout << "Enter coefficients in a single line: ";
cin >> coeffs.coeffs[0] >> coeffs.coeffs[1] >> coeffs.coeffs[2];
return coeffs;
}
////////////////////////////////////////////////////////////////////////
solver::rootStruct solver::calcRoots(double coeffs[]) {
rootStruct roots;
roots.roots[0] = ((-coeffs[1]+sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
roots.roots[1] = ((-coeffs[1]-sqrt((coeffs[1]*coeffs[1])-(4*coeffs[0]*coeffs[2])))/(2*coeffs[0]));
return roots;
}
////////////////////////////////////////////////////////////////////////
void solver::displayRoots(double roots[]) {
if (sqrt((coeffs[0]*coeffs[1]*coeffs[2])) >= 0) {
cout << "The solutions of a quadratic equation with coefficients: " << endl;
cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
cout << "are as follows " << endl;
cout << "Root 1: " << r1 << endl;
cout << "Root 2: " << r2 << endl;
cout << endl;
}
else {
cout << "The solution of a quadratic equation with coefficients: " << endl;
cout << "a = " << coeffs[0] << " b = " << coeffs[1] << " c = " << coeffs[2] << endl;
cout << "does not exist in the real domain. " << endl;
cout << endl;
}
}
////////////////////////////////////////////////////////////////////////
double solver::discr(double coeffs[]) {
double dscrResult = ((coeffs[0]*coeffs[1])-(4*coeffs[0]*coeffs[2]));
return dscrResult;
}
file: solverTest.cpp
Code:
#include "solver.h"
#include <iostream>
using namespace std;
int main(int Nc, char* Nv[]) {
solver solve;
solver::coeffStruct coeffs;
solver::rootStruct roots;
coeffs = solve.readCoeffs();
roots = solve.calcRoots(coeffs);
solve.discr(coeffs);
solve.displayRoots(roots);
}
compiled into putty by g++ -o solver solver.cpp solverTest.cpp
errors:
solver.cpp:24: error: prototype for âsolver::rootStruct solver::calcRoots(double*)â does not match any in class âsolverâ
solver.h:32: error: candidate is: solver::rootStruct solver::calcRoots(double)
solver.cpp:34: error: prototype for âvoid solver::displayRoots(double*)â does not match any in class âsolverâ
solver.h:36: error: candidate is: void solver::displayRoots(double)
solver.cpp:52: error: prototype for âdouble solver::discr(double*)â does not match any in class âsolverâ
solver.h:42: error: candidate is: double solver::discr(double)
solverTest.cpp: In function âint main(int, char**)â:
solverTest.cpp:12: error: no matching function for call to âsolver::calcRoots(solver::coeffStruct&)â
solver.h:32: note: candidates are: solver::rootStruct solver::calcRoots(double)
solverTest.cpp:14: error: no matching function for call to âsolver::discr(solver::coeffStruct&)â
solver.h:42: note: candidates are: double solver::discr(double)
solverTest.cpp:15: error: no matching function for call to âsolver::displayRoots(solver::rootStruct&)â
solver.h:36: note: candidates are: void solver::displayRoots(double)