我在编译与其他成功编译的函数类似的函数时遇到问题。这是错误:
University.cpp: In function ‘bool ValidCRN(long int)’:
University.cpp:113: error: ‘Courses’ was not declared in this scope
University.cpp:23: error: ‘int University::index’ is protected
University.cpp:117: error: within this context
University.cpp:22: error: ‘bool University::success’ is protected
University.cpp:118: error: within this context
University.cpp:21: error: ‘bool University::failure’ is protected
University.cpp:122: error: within this context
这是我的大学.h:
#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include <string>
#include <vector>
#include <iostream>
using namespace std;
#include "Department.h"
#include "Student.h"
#include "Course.h"
#include "Faculty.h"
#include "Person.h"
class University
{
friend ostream& operator<< (ostream& os, const vector<Department>& d);
friend ostream& operator<< (ostream& os, const Department& department);
friend ostream& operator<<(ostream& os, const vector<Faculty>& f);
friend ostream& operator<<(ostream& os, const Faculty& faculty);
friend ostream& operator<<(ostream& os, const vector<Student>& s);
friend ostream& operator<<(ostream& os, const Student& student);
friend ostream& operator<<(ostream& os, const Course& course);
friend ostream& operator<<(ostream& os, const vector<Course>& c);
protected:
vector<Department> Departments;
vector<Student> Students;
vector<Course> Courses;
vector<Faculty> Faculties;
static bool failure;
static bool success;
static int index;
public:
bool CreateNewDepartment(string dName, string dLocation, long dChairID);
bool ValidFaculty(long dChairID);
bool ValidDepartID(long fDepartID);
bool CreateNewFaculty(string pName, string pEmail, string pAddress, string pDateOfBirth, string pGender,float fSalary, int fYearOfExp, long fDepartID);
bool CreateNewStudent(string pName, string pEmail, string pAddress, string pDateOfBirth, string pGender, int sYearOfStudy, string sMajor, long sAdvisorID);
bool CreateNewCourse(int cMaxAvailableSeats, string cName, long cDepartID, long cAssignedSeats, long cIsTaughtBy);
bool ValidMajor(string sMajor);
bool ValidAdvisor(long sAdvisorID);
bool ValidInstructor(long cIsTaughtBy);
bool RemoveADepartment(long ID);
bool ValidDID(long ID);
bool RemoveAStudent(long ID);
bool ValidSID(long ID);
bool RemoveACourse(long CRN);
bool ValidCRN(long CRN);
bool RemoveAFaculty(long ID);
bool ValidFID(long ID);
};
#endif
我的 University.cpp 文件包含四个函数(前两个没有编译错误):
#include<string>
#include<vector>
#include<iostream>
using namespace std;
#include "University.h"
bool University::failure = false;
bool University::success = true;
int University::index; //This is line 23
bool University::RemoveADepartment(long ID)
{
if(ValidDID(ID) == University::success)
{
for (int i = 0; i < Departments.size(); i++)
Departments.erase(Departments.begin() + University::index);
cout << "Department " << ID << " deleted." << endl;
cout << Departments;
return University::success;
}
return University::failure;
}
bool University::ValidDID(long ID)
{
for (int i = 0; i < Departments.size(); i++)
{
if(Departments[i].ID == ID)
{
University::index = i;
return University::success;
}
}
cout << "Department not found." << endl;
return University::failure;
}
bool University::RemoveACourse(long CRN)
{
if (ValidCRN(CRN) == University::success)
{
for (int i = 0; i < Courses.size(); i++)
Courses.erase(Courses.begin() + University::index);
cout << "Course " << CRN << " deleted." << endl;
return University::success;
}
return University::failure;
}
bool ValidCRN(long CRN)
{
for(int i = 0; i < Courses.size(); i++) //This is Line 113
{
if (Courses[i].CRN == CRN)
{
University::index = i; //This is Line 117
return University::success;
}
}
cout << "Course " << CRN << " not found." <<endl;
return University::failure;
}
我不完全确定为什么会出现这些错误。我的课程向量不能成为问题。我能够成功地将它填充到一个函数中(未列出)。而且我一直在我的其他函数中使用我的静态变量,没有任何问题。知道为什么我会得到这个吗?