Help with classes & structures

This is a discussion on Help with classes & structures within the C++ Programming forums, part of the General Programming Boards category; I have to make a quadratic equation calculator out of classes and use structures with arrays. I'll post the code ...

  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
    2,916
    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
    1,625
    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 06: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
    1,625
    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
    2,916
    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
    1,625
    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
    2,916
    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
    1,625
    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
    2,916
    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 06: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.

Page 1 of 2 12 LastLast
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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21