基本上,该程序创建课程,在该课程中最多创建30名学生,召集每个学生的出勤率,并询问已创建的课程总数。我在最后一部分遇到了麻烦。
我在Student类中编写的getID()函数有效,但是当我与Course类中的howMany()函数进行类似操作时,出现错误。具体来说,我在Course.cpp的第23行(其中numInstances被告知在Course构造函数中增加一个)和Course.h的第28行(其中howMany( ) 被定义为)。
我怀疑这与包含静态变量的事实有关。
Student.h
##ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using std::string;
//This class describes a Student
class Student
{
protected:
//data
string name;
int ID;
public:
virtual string answer(); //allows polymorph
Student();
Student(string theName, int theID);
string toString();
string getName();
int getID(){ return ID; }
};
#endif //STUDENT_H
Student.cpp
#include <iostream>
#include <string>
using namespace std;
#include "Student.h"
//This class describes a Student
//default constructor
Student::Student()
{
name = "unknown";
ID = 0;
}
//parameterized constructor
Student::Student(string theName, int theID)
{
name = theName;
ID = theID;
}
//toString - returns String representation of the instance
string Student::toString()
{
string msg = name + ": \t";
msg += to_string(ID);
return msg;
}
//answer - returns how the Student instance answers during attendance
string Student::answer()
{
return "\"Here\""; //note use of escape character here...
}
//getName - returns the name
string Student::getName()
{
return name;
}
Course.h
#ifndef COURSE_H
#define COURSE_H
#include <iostream>
#include <string>
using std::string;
#include "Student.h"
const int MAX_STUDENTS = 30;
class Course
{
private:
//data
string name;
int maxCount;
int count;
Student *roster[MAX_STUDENTS];
public:
static int numInstances; //initialized in Course.cpp
Course();
string toString();
void takeAttendance();
void addStudent(Student *newStudent);
static int howMany() { return numInstances; }
};
#endif
Course.cpp
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
#include "Course.h"
static int numInstances = 0;
//default constructor - sets the maxCount to 30,
// initializes name/count, creates array,
// updates numInstances.
Course::Course()
{
name = "unknown";
maxCount = MAX_STUDENTS; //30
count = 0;
*roster[maxCount];
numInstances++;
}
Driver.cpp
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
#include "Student.h"
#include "Course.h"
//This is the driver class for the program
int main()
{
//create a Student
Student firstStudent("Joe", 13);
cout << "----- First Student is: \n";
cout << firstStudent.toString() << endl;
//tell it to return its ID - print it.
cout << "\n----- Ask the first Student for its ID: \n";
cout << firstStudent.getID() << endl;
cout << "\n----- Creating a Course and telling it to add some Students. Printing it:\n";
//create a new Course
Course algebra; //max of 30 students by default
//tell it to add some Students
algebra.addStudent(&firstStudent);
algebra.addStudent(new Student());
algebra.addStudent(new Student("Peter", 21));
algebra.addStudent(new Student("Janet", 43));
algebra.addStudent(new Student("Jim", 243));
algebra.addStudent(new Student("Al", 3));
algebra.addStudent(new Student());
algebra.addStudent(new Student("Jenn", 23));
algebra.addStudent(new Student("Jose", 43));
algebra.addStudent(new Student("Grant", 7));
//print the string representation of the course to see what is there
cout << algebra.toString() << endl;
//tell the course to take attendance
cout << "\n----- telling the course to take attendance\n";
algebra.takeAttendance();
//create two more new Courses
Course history;
Course spanish;
//ask the Course class how many have been created (static)
cout << "\n----- Asking the Course class how many courses have been created: \n";
cout << Course::howMany() << endl;
cin.get();
return 0;
}//end main()
代码转储的歉意。我试图减少线路,但遇到了更多错误,因此为了避免混淆,我保留了很多错误。问题(我相信)在Course.h和Course.cpp文件中。所制作课程的输出应为3
。