I get the following error when calling the class object and functions in a class instance:
1) fig7_17.cpp:
fig07_17.cpp
#include <array>
#include "GradeBook.h"
using namespace std;
// function main begins program execution
int main()
{
const array< int, GradeBook::students > grades =
{ 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 };
string courseName = "CS101 Introduction to C++ Programming";
GradeBook myGradeBook( courseName, grades );
myGradeBook.displayMessage();
myGradeBook.processGrades();
}
2) Gradebook.cpp :
// Fig. 7.16: GradeBook.cpp
#include <iostream>
#include <iomanip>
#include "GradeBook.h"
using namespace std;
GradeBook::GradeBook( const string &name,
const array< int, students > &gradesArray )
: courseName( name ), grades( gradesArray )
{
}
void GradeBook::setCourseName( const string &name )
{
courseName = name;
}
string GradeBook::getCourseName() const
{
return courseName;
}
void GradeBook::displayMessage() const
{
cout<< "Welcome to the grade book for\n" << getCourseName() << "!"
<< endl;
}
void GradeBook::processGrades() const
{
// output grades array
outputGrades();
// call function getAverage to calculate the average grade
cout << setprecision( 2 ) << fixed;
cout << "\nClass average is " << getAverage() << endl;
// call functions getMinimum and getMaximum
cout << "Lowest grade is " << getMinimum() << "\nHighest grade is "
<< getMaximum() << endl;
// call function outputBarChart to print grade distribution chart
outputBarChart();
} // end function processGrades
int GradeBook::getMinimum() const
{
int lowGrade = 100; // assume lowest grade is 100
// loop through grades array
for ( int grade : grades )
{
// if current grade lower than lowGrade, assign it to lowGrade
if ( grade < lowGrade )
lowGrade = grade; // new lowest grade
} // end for
return lowGrade; // return lowest grade
} // end function getMinimum
int GradeBook::getMaximum() const
{
int highGrade = 0; // assume highest grade is 0
// loop through grades array
for ( int grade : grades )
{
// if current grade higher than highGrade, assign it to highGrade
if ( grade > highGrade )
highGrade = grade; // new highest grade
} // end for
return highGrade; // return highest grade
} // end function getMaximum
// determine average grade for test
double GradeBook::getAverage() const
{
int total = 0; // initialize total
// sum grades in array
for ( int grade : grades )
total += grade;
// return average of grades
return static_cast< double >( total ) / grades.size();
} // end function getAverage
void GradeBook::outputBarChart() const
{
cout << "\nGrade distribution:" << endl;
// stores frequency of grades in each range of 10 grades
const size_t frequencySize = 11;
array< unsigned int, frequencySize > frequency = {}; // init to 0s
// for each grade, increment the appropriate frequency
for ( int grade : grades )
++frequency[ grade / 10 ];
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 GradeBook::outputGrades() const
{
cout << "\nThe grades are:\n\n";
// output each student's grade
for ( size_t student = 0; student < grades.size(); ++student )
cout << "Student " << setw( 2 ) << student + 1 << ": " << setw( 3 )
<< grades[ student ] << endl;
}
3) GradeBook.h:
#include <string>
#include <array>
class GradeBook
{
public:
// constant -- number of students who took the test
static const size_t students = 10; // note public data
// constructor initializes course name and array of grades
GradeBook( const std::string &, const std::array< int, students > & );
void setCourseName( const std::string & );
std::string getCourseName() const;
void displayMessage() const;
void processGrades() const;
int getMinimum() const;
int getMaximum() const;
double getAverage() const;
void outputBarChart() const;
void outputGrades() const;
private:
std::string courseName;
std::array< int, students > grades;
};
Error:
C:\Users\PC\AppData\Local\Temp\cciKxoUd.o:fig07_17.cpp:(.text+0xa5): undefined reference to `GradeBook::GradeBook(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::array<int, 10u> const&)'
C:\Users\PC\AppData\Local\Temp\cciKxoUd.o:fig07_17.cpp:(.text+0xb5): undefined reference to `GradeBook::displayMessage() const'
C:\Users\PC\AppData\Local\Temp\cciKxoUd.o:fig07_17.cpp:(.text+0xc2): undefined reference to `GradeBook::processGrades() const'
collect2.exe: error: ld returned 1 exit status
[Finished in 330ms]
I may have used wrong terms because I did not understand some situations. But I fixed it. I'm sorry. I think the written code is correct. I use sublime text by the way.
I tried to access the class members and functions but I got a error.(how to program C++ - Exeample 7_15_17)