Thread: Class function problem

  1. #1
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53

    Class function problem

    I've started a class ScoreBank that will take in test scores and (eventually) average them...but I can't seem to get past this error:

    Code:
    class ScoreBank { // base class ScoreBank
    
    public:
       ScoreBank( int = 0 ); // default constructor
       ~ScoreBank(); // destructor
    
       void EnterScores( float );
       int GetScores() const;
       void CalcAverage( float );
       void OutPut() const; // print ScoreBank object
    
    private:
       float ptrScore;
       float average;
       int TestScores; // how many test scores to be entered?
       float *ptrScore; 
       float total; 
    
    }; // end class ScoreBank
    
    // default constructor
    ScoreBank::ScoreBank( float _score )
       : ptrScore( _score )
    {
    } // end ScoreBank constructor
    
    // destructor
    ScoreBank::~ScoreBank()
    {
    } // end ScoreBank destructor
    
    void ScoreBank::EnterScores( int _TestScores )
    {
       cout << "How many test scores are needed? ";
       cin >> TestScores;
       TestScores = _TestScores;
       ptrScore = new float[TestScores];
    
    } // end function EnterScores
    I'm sure it has something to do with the arguments (the constructor expects a _score, but EnterScores() expects _TestScores) however I'm not sure how to proceed. Any help is appreciated.

    By the way, function Main has:

    Code:
       ScoreBank score( 98 );
       score.OutPut();
    Where 98 is supposed to be typed in by the user.
    THE redheaded stepchild.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    132
    i might be wrong and might not be anything to do with it but...

    u set Enterscore ( FLOAT ) not int
    then at bottom u set Enterscore ( INT Testscore )

    it prolly dont matter but i am not sure hehe.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you declared the constructor as
    Code:
       ScoreBank( int = 0 ); // default constructor
    but you implemented

    Code:
    // default constructor
    ScoreBank::ScoreBank( float _score )
       : ptrScore( _score )
    {
    } // end ScoreBank constructor
    Guess your constructor shouldn't take any parameters because you actually have a function to ask the user for the number of TestScores. EnterScores() should also not take any parameters.

    Code:
    // default constructor
    ScoreBank::ScoreBank( )
       : TestScores( 0 ),
       ptrScore( 0 )
    {
    } 
    void ScoreBank::EnterScores( )
    {
       cout << "How many test scores are needed? ";
       cin >> TestScores;
       ptrScore = new float[TestScores];
       // here you should put some  values into the array.
       // possibly by calling 
       GetScores();
    
    } // end function EnterScores
    use it this way
    Code:
       ScoreBank score;
       score.EnterScores();
       score.OutPut();
    BTW GetScores() cannot be const because it will modify the object.

    Kurt

  4. #4
    Registered User Kayoss's Avatar
    Join Date
    Sep 2005
    Location
    California
    Posts
    53
    Thank you Kurt! I believe that pointed me in the right direction. Now I'm not sure what is going on...

    Code:
    #include<iostream>
    using namespace std;
    
    int scoreCount = 0;
    
    class ScoreBank { // base class ScoreBank
    
    public:
       ScoreBank( void ); // default constructor
       ~ScoreBank(); // destructor
    
       void EnterScores();
       float GetScores() const;
       void CalcAverage();
       void OutPut() const; // print ScoreBank object
    
    private:
        float average;
        float ptrScore;
        float total; // total test scores
    }; // end class ScoreBank
    
    
    // default constructor
    ScoreBank::ScoreBank()
       : ptrScore( 0 )
    {
    } // end ScoreBank constructor
    
    // destructor
    ScoreBank::~ScoreBank()
    {
    } // end ScoreBank destructor
    
    void ScoreBank::EnterScores()
    {
        int count;
        float *ptrScore;
        cout << "How many test scores are needed? ";
        cin >> scoreCount;
        ptrScore = new float[scoreCount]; // Allocate memory
    
        cout << "Enter test scores:\n";
        for (count = 0; count < scoreCount; count++)
       {
           cout << "Student " << (count + 1) << ": ";
           cin >> ptrScore[count];
           cout << endl;
       }
    
    } // end function EnterScores
    
    float ScoreBank::GetScores() const
    {
    
       return ptrScore;
    
    } // end function GetScores
    
    void ScoreBank::CalcAverage()
    {
        // Calculate the total Scores
        int count;
        for (count = 0; count < scoreCount; count++)
       {
           total += ptrScore[count];
       }
    
       cout << "This calculates average: ";
       average = total / scoreCount;
       cout << average << endl;
    } // end function CalcAverage
    
    void ScoreBank::OutPut() const
    {
       cout << "ScoreBank OutPut: ";
       cout << GetScores() << endl;
    
    } // end function OutPut()
    
    int main()
    {
    
       ScoreBank score();
       score.EnterScores();
       score.OutPut();
    
    	return 0;
    } // end main
    Last edited by Kayoss; 06-09-2006 at 04:08 PM.
    THE redheaded stepchild.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> total += ptrScore[count];
    You didn't provide the error message.

    >> ScoreBank score();
    You didn't implement Kurt's suggestion correctly. That code declares a function, but you want to declare a variable.
    Last edited by Daved; 06-09-2006 at 08:13 PM.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #include<iostream>
    #include<iomanip>   // for output formatting
    
    using namespace std;
    
    // int scoreCount = 0;  // **** should be a member variable 
    
    class ScoreBank { // base class ScoreBank
    
    public:
       ScoreBank( void ); // default constructor
       ScoreBank( const ScoreBank & other ); // *** this class needs a copy constructor
                                             // *** and an assignement operator to be useful
                                             // *** the one the compiler provides
                                             // *** would only copy the member variables leading to ugly bugs
       ScoreBank & operator = (const ScoreBank & other );
       
       virtual ~ScoreBank(); // *** I prefer virtual a destructor
    
       void EnterScores();
       float * GetScores() const;  // **** we want to return all of them
                                   // **** normally not a good idea the values could be
                                   // **** modified from outside the class
       void CalcAverage();
       void OutPut() const; // print ScoreBank object
    
    private:
        float average;
        float * ptrScore;   // **** shoud be an array
        float total; // total test scores
        
        int scoreCount;
        
    }; // end class ScoreBank
    
    
    // default constructor
    ScoreBank::ScoreBank()
        : ptrScore( 0 ),
        average(0.0),        // *** best place to initialize member variables
        scoreCount(0),
        total(0.0)
    {
    } // end ScoreBank constructor
    
    //copy constructor
    ScoreBank::ScoreBank( const ScoreBank & other )
        : ptrScore( 0 ),
        average(other.average ),
        scoreCount(other.scoreCount),
        total( other.total )
    {
        ptrScore = new float[scoreCount];    // Allocate memory
        // *** and copy the data
        for (int count = 0; count < scoreCount; count++) {
            ptrScore[count]= other.ptrScore[count];
        }
        
    }
    
    // assignement operator
    ScoreBank & ScoreBank::operator = (const ScoreBank & other ) {
        // *** I leave the implementation for you
        
        // *** should return a reference to this
        return * this;
    }
    
    // destructor
    ScoreBank::~ScoreBank()
    {
        // **** the data has to be released here
        delete [] ptrScore;
    } // end ScoreBank destructor
    
    void ScoreBank::EnterScores()
    {
        int count;
        // float *ptrScore; // *** this is a member variable
        cout << "How many test scores are needed? ";
        cin >> scoreCount;
        ptrScore = new float[scoreCount]; // Allocate memory
    
        cout << "Enter test scores:\n";
        for (count = 0; count < scoreCount; count++) {
           cout << "Student " << (count + 1) << ": ";
           cin >> ptrScore[count];
           cout << endl;
        }
    
    } // end function EnterScores
    
    float * ScoreBank::GetScores() const
    {
       return ptrScore;
    
    } // end function GetScoress
    
    void ScoreBank::CalcAverage()
    {
        // Calculate the total Scores
        int count;
        for (count = 0; count < scoreCount; count++)
    {
           total += ptrScore[count];
    }
    
       cout << "This calculates average: ";
       average = total / scoreCount;
       cout << average << endl;
    } // end function CalcAverage
    
    void ScoreBank::OutPut() const
    {
       cout << "ScoreBank OutPut: ";
       cout << GetScores() << endl;   // **** this would show only the pointer to the data
                                      // **** let's output all the values to make it more useful
       for ( int i = 0; i < scoreCount; ++i )
           cout << setw(3) << i << ":" << ptrScore[i] << endl;
    
    } // end function OutPut()
    
    int main()
    {
    
        // ScoreBank score();   // **** this would declare a function called score that returns a ScoreBank object
       ScoreBank score;
         
       score.EnterScores();
       score.OutPut();
       score.CalcAverage();   // *** it's there so why not use it
    
       return 0;
    } // end main
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM