. getaverage在它的左边需要一个类

.getAverage needs a class to the left of it

本文关键字:一个 左边 getaverage      更新时间:2023-10-16

我一直得到一个错误,说" .getAverage需要一个类在它的左边"。

这是赋值提示符:

考试分数。编写一个名为TestScores的类。类构造函数应该接受一个考试分数数组作为参数。类应该有一个成员函数,返回考试成绩的平均值。如果数组中的任何测试分数为负或大于100,则该类应抛出异常。

异常应该有两个类;一个叫NegativeScore,另一个叫TooLargeScore。这些异常类将具有一个数据成员,该数据成员是一个称为score的整数值。该数据成员将通过参数在构造函数中设置。它还应该提供一个名为getScore的成员函数,该函数返回分数数据成员。

TestScores中称为getAverages的函数将计算数组中测试分数的平均值(作为双精度)。它还将检查分数是否为负或大于100。如果它是负的,它应该使用NegativeScore类抛出异常。如果分数大于100,则应该使用TooLargeScore类抛出异常。

Main将创建TestScores类的实例并捕获异常。因此,它需要处理两个异常,并显示带有无效分数的错误消息。

#pragma once
/*
Specification file: TestScores.h
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#include <iostream>
#include <iomanip>
#include "NegativeScore.h"
#include "TooLargeScore.h"
using namespace std;
#ifndef TESTSCORES_H
#define TESTSCORES_H
const int SIZE = 5;
class TestScores
{
private:
    int testScore[];
public:
    //Exception Class 
    class NegativeScore
    {};
    //Exception Class
    class TooLargeScore
    {};
    //Default Constructor
    TestScores();
    //Constructor
    TestScores(int [], int SIZE);
    //Accessor/getter functions
    //getAverage accessor function
    double getAverage(int []);
};
#endif
[/code]
[code]
/*
Class Implementation Page: TestScores.cpp
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#include "TestScores.h"
//*******************************************
//Default Constructor that initializes the  *
//array of scores with zero's.              *
//*******************************************
TestScores::TestScores()
{
    testScore[0] = 0;
    testScore[1] = 0;
    testScore[2] = 0;
    testScore[3] = 0;
    testScore[4] = 0;
    testScore[5] = 0;
}
//********************************************
//The TestScores Constructor accepts an      *
//array of test scores as its argument.      *
//********************************************
TestScores::TestScores(int scoreArr[], int size)
{
    for (int i = 0; i < size; i++)
        testScore[i] = scoreArr[i];
}
//********************************************
//The getAverage function returns the average*
//of all the test scores, and throws an      *
//exception if one is negative or above 100. *
//********************************************
double TestScores::getAverage(int score[])
{
    double total = 0;
    double number = 0;
    for (int i = 0; i < SIZE; i++)//***
    {
        if (score[i] < 0)
        {
            throw NegativeScore();
        }
        else if (score[i] > 100)
        {
            throw TooLargeScore();
        }
        else
        {
            number = score[i];
        }
        total += number;
    }
    return total / SIZE;//***
}
[/code]
[code]
#pragma once
/*
Specification file: NegativeScore.h
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#ifndef NEGATIVESCORE_H
#define NEGATIVESCORE_H
//Exception Class 
class NegativeScore
{
private:
    int score;
public:
    NegativeScore(int);
    int getScore();
};
#endif
[/code]
[code]
/*
Class Implementation Page: NegativeScore.cpp
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#include "NegativeScore.h"
NegativeScore::NegativeScore(int s)
{
    score = s;
}
int NegativeScore::getScore()
{
    return score;
}
[/code]
[code]
#pragma once
/*
Specification file: TooLargeScore.h
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#ifndef TOOLARGESCORE_H
#define TOOLARGESCORE_H
//Exception Class
class TooLargeScore
{
private:
    int score;
public:
    TooLargeScore(int);
    int getScore() const;
};
#endif
[/code]
[code]
/*
Class Implementation Page: TooLargeScore.cpp
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#include "TooLargeScore.h"
TooLargeScore::TooLargeScore(int s)
{
    score = s;
}
int TooLargeScore::getScore() const
{
    return score;
}
[/code]
[code]
/*
Main Program Page: Source.cpp
Program: P3W3A1
Author: Joseph Bales
Created: 9/15/16
Revised: 9/15/16
*/
#include "TestScores.h"

int main()
{
    //Array to hold scores
    int score[5];
    //get test scores from user
    cout << "Please Enter " << SIZE << " test scores: ";
    for (int i = 0; i < SIZE; i++)
    {
        cin >> score[i];
    }
    //Check for exceptions
    try
    {
        ///HERE IS THE ERROR.
        TestScores object(int score[], int SIZE);
        cout << "The average of the scores is " << object.getAverage() << endl;
    }
    catch (TestScores::TooLargeScore)
    {
        cout << "Error: The value entered was too high.n";
    }
    catch (TestScores::NegativeScore)
    {
        cout << "Error: A Negative value was entered.n";
    }
    cout << "End of the program.n";
    return 0;
}
[/code]
///HERE IS THE ERROR.
TestScores object(int score[], int SIZE);
cout << "The average of the scores is " << object.getAverage() << endl;

没有声明类型为TestScores的新对象object。相反,您已经向前声明了一个名为object的函数原型,它返回类型为TestScores的值。

去掉类型定义,使用实际的标识符:

TestScores object( score, SIZE );
cout << "The average of the scores is " << object.getAverage( score ) << endl;

编辑:我注意到你在你的构造函数中有未定义的行为。但这超出了你最初问题的范围。但是你可能要考虑给testScores一个大小

in main通过在参数列表中指定int来声明函数原型,TestScores对象(int score[], int SIZE);

改为:TestScores对象(得分、大小),