У меня возникла проблема с компиляцией функции, аналогичной другим успешно скомпилированным функциям. Здесь ошибка:Ошибка при компиляции функции
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
Вот мой University.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;
}
Я не совсем уверен, почему я получаю эти ошибки. Мой курс не может быть проблемой. Я смог успешно заполнить его в функции (не указан). И я использовал свои статические переменные в своих других функциях без каких-либо проблем. Любая идея, почему я получаю это?
Я чувствую себя идиотом! Я смотрел на это снова и снова, и мне не раз приходилось смотреть на это. – Kevin
Большинство ошибок очевидны с учетом ретроспективного внимания :). Для дальнейшего использования ошибки, такие как 'int University :: index protected ', являются хорошим намеком на то, что функция не рассматривается как часть вашего класса. – simonc
Хороший способ сделать такую ошибку более очевидной - использовать отличитель для переменных-членов, например. «m_courses», статические переменные «s_localCounter» и глобальные «g_shuttingDown». – kfsone