我遇到了有关非静态成员无效使用的错误以及来自结构体成员未正常工作的其他问题,我很难理解问题所在,谢谢。
#include <iostream>
#include <string>
using namespace std;
struct classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int ListOfStudents[NumberOfStudents];
string LectureName;
bool Window, Projector, Available;
}classroom;
int main() {
cout << "Please enter your room number" << endl;
cin >> classroom.RoomNumber;
cout << "Enter the name of the Lecture" << endl;
cin >> classroom.LectureName;
cout << "Enter number of students" << endl;
cin >> classroom.NumberOfStudents;
cout << "Enter " << classroom.NumberOfStudents << " Student Names" << endl;
cin >> classroom.ListOfStudents;
cout << "Enter number of chairs" << endl;
cin >> classroom.NumberOfChairs;
cout << "Are there any Windows? (Y/N)" << endl;
cin >> classroom.Window;
cout << "Are there any Projectors? (Y/N)" << endl;
cin >> classroom.Projector;
cout << "Are there any Available? (Y/N)" << endl;
cin >> classroom.Available;
return 0;
}
错误
prog.cpp:10:5: error: invalid use of non-static data member ‘classroom::NumberOfStudents’
int NumberOfStudents;
^
prog.cpp:11:20: error: from this location
int ListOfStudents[NumberOfStudents];
^
prog.cpp: In function ‘int main()’:
prog.cpp:28:18: error: ‘struct classroom’ has no member named ‘ListOfStudents’
cin >> classroom.ListOfStudents;
^
struct ClassRoom
和classroom
。话虽如此,您需要检查名称查找规则,以确定编译器是否正确地将 main 中的classroom
视为类型。 - Keith