2

我收到有关无效使用非静态成员的错误以及结构成员无法正常工作的其他问题,我无法理解问题所在,谢谢。

#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;
                  ^
4

4 回答 4

10

You can't declare the array int ListOfStudents[NumberOfStudents] unless NumberOfStudents is of type const int. If it is variable (ie. not const), then the compiler doesn't know how to allocate space for your array. So just change int NumberOfStudents; to const int NumberOfStudents;. However when you do this, your struct will also expect NumberOfStudents to be static, so you'll actually need to write static const int NumberOfStudents;.

At this point, you won't be able to cin >> classroom.NumberOfStudents, and things get pretty ugly. I'm assuming this isn't what you want to do.

If you do want a variable-sized array, then the standard way of doing this is to use heap allocation. In other words, you need to declare ListOfStudents to be a pointer to an integer array, which you will allocate during runtime.

That is, you want to change your struct as follows:

struct classroom {
int RoomNumber;
int NumberOfChairs;
int NumberOfStudents;
int* ListOfStudents;
string LectureName;
bool Window, Projector, Available;
}classroom;

Then inside your main function (or in fact wherever you like), you'll need to call new to allocate some space. Like this:

classroom.ListOfStudents = new int[classroom.NumberOfStudents];

One last note: Doing this, you'll also need to change your cin >> classroom.ListOfStudents; to a loop that will read all the values into your array. Like this:

for (int i=0; i < classroom.NumberOfStudents; i++) {
  cin >> classroom.ListOfStudents[i];
}

As suggested by the other answers, it would also be a good idea to change the name of your variable classroom so it doesn't match the name of the struct. However it should still compile just fine (I tested it). It's just a bit confusing.

于 2013-10-08T06:03:51.547 回答
4

C++ 不支持 VLA(可变长度数组),因此您不应使用 ListOfStudents[NumberOfStudents]。改用 const

编辑:

prog.cpp:28:18: error: ‘struct classroom’ has no member named ‘ListOfStudents’
 cin >> classroom.ListOfStudents;

此错误表示没有与 &int 类型匹配的运算符>>。您可能想要循环并打印数组的每个元素。

于 2013-10-08T05:58:20.047 回答
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;
}
于 2013-10-08T05:54:26.193 回答
0

您不能拥有与结构同名的变量,请将结构底部的“教室”更改为其他内容。或者使结构“教室”。

struct Classroom {
    int RoomNumber;
    int NumberOfChairs;
    int NumberOfStudents;
    int ListOfStudents[NumberOfStudents];
    string LectureName;
    bool Window, Projector, Available;
}somethingelse;

您还可以使用以下方法在 main 中创建一个 struct 类型的变量:

classroom someClassroom;
于 2013-10-08T05:55:41.963 回答