我收到有关非静态成员的无效使用以及结构成员无法正常工作的其他问题的错误,我无法理解问题所在,谢谢。
#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;
^
答案 0 :(得分:7)
除非int ListOfStudents[NumberOfStudents]
的类型为NumberOfStudents
,否则您无法声明数组const int
。如果它是可变的(即不是const
),则编译器不知道如何为数组分配空间。因此,只需将int NumberOfStudents;
更改为const int NumberOfStudents;
即可。但是,当您执行此操作时,您的结构体也会期望NumberOfStudents
是静态的,因此您实际上需要编写static const int NumberOfStudents;
。
此时,您将无法cin >> classroom.NumberOfStudents
,事情变得相当丑陋。我假设这不是你想要做的。
如果您确实需要可变大小的数组,那么执行此操作的标准方法是使用堆分配。换句话说,您需要将ListOfStudents
声明为指向整数数组的指针,您将在运行时分配该数组。
也就是说,您想要按如下方式更改结构:
struct classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int* ListOfStudents;
string LectureName;
bool Window, Projector, Available;
}classroom;
然后在你的main
函数内(或者实际上你喜欢的任何地方),你需要调用new
来分配一些空间。像这样:
classroom.ListOfStudents = new int[classroom.NumberOfStudents];
最后一点注意:执行此操作时,您还需要将cin >> classroom.ListOfStudents;
更改为将所有值读入数组的循环。像这样:
for (int i=0; i < classroom.NumberOfStudents; i++) {
cin >> classroom.ListOfStudents[i];
}
正如其他答案所建议的那样,更改变量classroom
的名称也是一个好主意,因此它与结构的名称不匹配。但它应该仍然编译得很好(我测试了它)。这有点令人困惑。
答案 1 :(得分:4)
C ++不支持VLA(可变长度数组),因此不应使用ListOfStudents [NumberOfStudents]。改为使用const
编辑:
prog.cpp:28:18: error: ‘struct classroom’ has no member named ‘ListOfStudents’
cin >> classroom.ListOfStudents;
此错误表示没有运算符&gt;&gt;匹配类型&amp; int。您可能想要循环并打印数组的每个元素。
答案 2 :(得分:1)
你可以这样做,在你的函数中创建一个局部变量(因为建议不要使用全局变量):
struct classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int ListOfStudents[NumberOfStudents];
string LectureName;
bool Window, Projector, Available;
};
int main(){
classroom myClassRoom;
cout << "Please enter your room number" << endl;
cin >> myClassRoom.RoomNumber;
cout << "Enter the name of the Lecture" << endl;
cin >> myClassRoom.LectureName;
cout << "Enter number of students" << endl;
cin >> myClassRoom.NumberOfStudents;
cout << "Enter " << myClassRoom.NumberOfStudents << " Student Names" << endl;
cin >> myClassRoom.ListOfStudents;
cout << "Enter number of chairs" << endl;
cin >> myClassRoom.NumberOfChairs;
cout << "Are there any Windows? (Y/N)" << endl;
cin >> myClassRoom.Window;
cout << "Are there any Projectors? (Y/N)" << endl;
cin >> myClassRoom.Projector;
cout << "Are there any Available? (Y/N)" << endl;
cin >> myClassRoom.Available;
return 0;
}
或者,如果您需要全局变量,请更改编译器的名称以使结构和变量名称之间存在差异,然后在main()
函数中使用myClassRoom
代替它classroom
:
struct classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int ListOfStudents[NumberOfStudents];
string LectureName;
bool Window, Projector, Available;
}myClassRoom;
编辑:您将ListOfStudents
的长度设置为固定数字,例如
int ListOfStudents[512];
然后,这段代码在我的VS中编译:
struct classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int ListOfStudents[200];
string LectureName;
bool Window, Projector, Available;
};
int main(){
classroom myClassRoom;
cout << "Please enter your room number" << endl;
cin >> myClassRoom.RoomNumber;
cout << "Enter the name of the Lecture" << endl;
cin >> myClassRoom.LectureName;
cout << "Enter number of students" << endl;
cin >> myClassRoom.NumberOfStudents;
cout << "Enter " << myClassRoom.NumberOfStudents << " Student Names" << endl;
for(int i = 0; i < myClassRoom.NumberOfStudents; ++i)
{
cin >> myClassRoom.ListOfStudents[i];
}
cout << "Enter number of chairs" << endl;
cin >> myClassRoom.NumberOfChairs;
cout << "Are there any Windows? (Y/N)" << endl;
cin >> myClassRoom.Window;
cout << "Are there any Projectors? (Y/N)" << endl;
cin >> myClassRoom.Projector;
cout << "Are there any Available? (Y/N)" << endl;
cin >> myClassRoom.Available;
return 0;
}
答案 3 :(得分:0)
您不能拥有与结构相同名称的变量,将结构底部的“教室”更改为其他内容。或者使结构“课堂”。
struct Classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int ListOfStudents[NumberOfStudents];
string LectureName;
bool Window, Projector, Available;
}somethingelse;
您还可以使用以下方法在main中创建struct类型的变量:
classroom someClassroom;