. getaverage在它的左边需要一个类
.getAverage needs a class to the left of it
我一直得到一个错误,说" .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对象(得分、大小),
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '