Thread: Help with classes & structures

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    16

    Help with classes & structures

    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)

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    solver.cpp:24: error: prototype for âsolver::rootStruct solver::calcRoots(double*)â does not match any in class âsolverâ
    Your function definition and function implementation do not match.
    Your function definition is
    Code:
    rootStruct calcRoots(double);
    and your implementation is
    Code:
    solver::rootStruct solver::calcRoots(double coeffs[])
    These must match exactly.

    Jim

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Careful: Quadratic equation is a*x^2 + b*x + c = 0 (1)

    If "a" == 0 then it becomes: b*x + c = 0 (2)
    (2)'s solution is: x = -c/b , for "b" != 0

    To solve (1), you have to get its Delta?( Δ ):
    Δ = b^2 - 4*a*c
    Then:
    x = (-b +/- sqrt(Δ))/(2*a) , for "Δ" >= 0

    Remember that if conditions arenot met, the equation has no solution. ( In R anyway )
    Last edited by GReaper; 04-10-2011 at 05:21 PM.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Thanks! What about the solverTest.cpp file though. I can't get them to match up equally like that without getting errors. Since it's calling the functions I assumed it wouldn't work anyways, but what do I do about those?

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    As @jimblumberg said, your functions' prototypes/declarations and their definitions must be exactly the same!
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Well making it exactly the same for calling the function is giving me errors aswell, thats why I asked if it was different in that case. So can you give me an example of how one of my function calls should look like? Mabye I'm doing them wrong in the first place. I got it to work in the solver.cpp file just not the solverTest.cpp file

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What are the error messages when you tried to make the changes, plus post the code where you tried.

    Jim

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    My changed solver.h file which now matches my solver.cpp file.


    // 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 coeffs[]);

    // Function to display values of the roots or an error message,
    // if the roots do not exist
    void displayRoots(double roots[], double coeffs[]);



    private:
    // Function to calculate the discriminant
    double discr(double coeffs[]);
    };

    #endif // COUNTER_H


    and my solverTest.cpp file


    #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(double coeffs[]);

    solve.discr(double coeffs[]);
    solve.displayRoots(double roots[], double coeffs[]);
    }


    my errors


    solverTest.cpp:12: error: expected primary-expression before âdoubleâ
    solverTest.cpp:14: error: expected primary-expression before âdoubleâ
    solverTest.cpp:15: error: expected primary-expression before âdoubleâ
    solverTest.cpp:15: error: expected primary-expression before âdoubleâ

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, i didn't mean to be exactly the same when you call them!
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Code:
    roots = solve.calcRoots(double coeffs[]);
    This is not how you call a function. Please see these links: Functions I and Functions II

    Jim

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    roots = solve.calcRoots(coeffs); doesn't work either, which seems right to me which is what I origonally had.
    coeffs = solve.readCoeffs(); works and its the same method. I just can't seem to see what I'm missing

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by PenguinTux View Post
    roots = solve.calcRoots(coeffs); doesn't work either, which seems right to me which is what I origonally had.
    your "solver::calcRoots" takes "double[]", not "coeffStruct"!
    Devoted my life to programming...

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Your functions are expecting arrays of doubles so why are you trying to send them coeffStruct instead of an array of doubles?

    Code:
    solver::rootStruct roots;
    solver::coeffStruct coeffs;
    
    coeffs = solve.readCoeffs();
    roots = solve.calcRoots(double coeffs[]);

    Jim
    Last edited by jimblumberg; 04-10-2011 at 05:55 PM.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    what am I doing that's making it take coeffstruct? and what can i do to make it take double?

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    16
    Ah I see how it's taking it. The trouble now though is it says coeffs and roots isn't declared in this scope.

    I changed solver::coeffStruct coeffs; to solver::coeffStruct coeff; and solver::rootStruct roots; to solver::rootStruct root; that seemed to have worked.

    but my arrays coeffs and roots are not defined in this scope.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with structures and classes
    By jdcollins in forum C++ Programming
    Replies: 1
    Last Post: 11-14-2009, 05:07 PM
  2. Classes and Structures.
    By jrahhali in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2004, 05:03 PM
  3. Classes and structures
    By mcorn in forum C++ Programming
    Replies: 19
    Last Post: 12-12-2002, 11:50 AM
  4. classes and structures
    By mcorn in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2002, 05:08 PM
  5. structures and classes
    By abrege in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2002, 03:26 AM