我用C ++编写了一个程序,用于考试报告。我没有在代码中看到任何错误,但每次执行代码时,都会出现以下错误:
#ifndef _CXX0X_WARNING_H
#define _CXX0X_WARNING_H 1
#if __cplusplus < 201103L
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
#endif
#endif
我在这个程序中使用了3个文件..一个头文件和2个cpp源文件。
以下是我的头文件ExamReport.h
#ifndef EXAMREPORT_H_
#define EXAMREPORT_H_
#include <string>
#include <array>
class ExamReport {
public:
static const size_t studentNum = 10;
ExamReport(const std::string &, const std::array<int, studentNum> &);
void setModuleName(const std::string &);
std::string getModuleName() const;
void displayMessage() const;
void processMarks() const;
int getMinimum() const;
int getMaximum() const;
double getAverage() const;
void outputBarChart() const;
void outputMarks() const;
private:
std::string moduleName; // module name
std::array<int, studentNum> marks; // array of student marks
};
#endif
这是我的ExamReport.cpp(类操作)文件。
#include <iostream>
#include <iomanip>
#include "ExamReport.h"
using namespace std;
ExamReport::ExamReport(const string &name, const array<int, studentNum> &marksArray) :
moduleName(name), marks(marksArray) {
}
void ExamReport::setModuleName(const string &name) {
moduleName= name; // store the module name
}
string ExamReport::getModuleName() const {
return moduleName;
}
void ExamReport::displayMessage() const {
cout << "Exam Report for\n"<< getModuleName() << "!"<< endl;
}
void ExamReport::processMarks() const {
/* output marks array*/
outputMarks();
/* call function getAverageto calculate the average mark */
cout<< setprecision(2) << fixed;
cout<< "\nClassaverage is "<< getAverage() << endl;
/* call functions getMinimumand getMaximum*/
cout<< "Lowest mark is "<< getMinimum() << "\nHighestmark is "
<< getMaximum() << endl;
/* call function outputBarChartto print mark distribution chart*/
outputBarChart();
}
int ExamReport::getMinimum() const {
int lowMark= 100; // assume lowest mark is 100
/* loop through marks array */
for(int mark : marks) {
// if current mark lower than lowMark, assign it to lowMark
if (mark < lowMark)
lowMark = mark; // new lowest mark
}
return lowMark; // return lowest mark
}
int ExamReport::getMaximum() const {
int highMark= 0; // assume highest mark is 0
// loop through marks array
for (int mark : marks) {
// if current mark higher than highMark, assign it to highMark
if (mark > highMark)
highMark= mark; // new highest mark
}
return highMark; // return highest mark
}
double ExamReport::getAverage() const {
int total = 0; // initialize total
// sum marks in array
for (int mark : marks )
total += mark;
// return average of marks
return static_cast<double>(total) / marks.size();
}
void ExamReport::outputBarChart() const {
cout<< "\nMarkdistribution:"<< endl;
/* stores frequency of marks in each range of 10 marks*/
const size_t frequencySize= 11;
array<unsigned int, frequencySize > frequency = { }; // init to 0s
/* for each mark, increment the appropriate frequency*/
for(int mark : marks)
++frequency[mark / 10];
/* for each mark frequency, print bar in chart*/
for(size_t count = 0; count < frequencySize; ++count) {
/* output bar labels ("0-9:", ..., "90-99:", "100:" )*/
if (0 == count)
cout << " 0-9: ";
else if (10 == count)
cout<< " 100: ";
else
cout<< count * 10 << "-"<< (count * 10) + 9 << ": ";
/* print bar of asterisks*/
for (unsigned int stars = 0; stars < frequency[count]; ++stars)
cout<< '*';
cout<< endl; // start a new line of output
}
}
void ExamReport::outputMarks() const {
cout<< "\nThemarks are:\n\n";
/* output each student's mark*/
for(size_t student = 0; student < marks.size(); ++student)
cout<< "Student "<< setw(2) << student + 1 << ": "<< setw(3)<<
marks[student] << endl;
}
这是我的第三个文件,其中包含主要功能TestMain.cpp
#include <array>
#include "ExamReport.h"
using namespace std;
int main() {
// array of student marks
const array<int, ExamReport::studentNum> marks = { 85,63,92,100,73,78,85,66,76,45};
string moduleName= "ECS769P Advanced Object-Oriented Programming";
ExamReport myExamReport(moduleName, marks);
myExamReport.displayMessage();
myExamReport.processMarks();
}
注意:我正在使用CodeLite IDE(版本9.0.7)来执行此程序