4

I am getting errors about Invalid use of non-static members as well as other issues from the struct members not working properly, I am having trouble understanding what the problem is, thanks.

#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;
}

Errors

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;
                  ^
ssj3goku878
  • 745
  • 3
  • 14
  • 43
  • 1
    I'd be inclined to give the type and the instance different names to avoid confusion. E.g. `struct ClassRoom` and `classroom`. Having said that, you'd need to check the name look-up rules to see if the compiler is right in thinking `classroom` in main refers to the type. – Keith Oct 08 '13 at 05:53
  • As tristan said you should go for an array with const size or for some standard containers like vectors or lists – sajas Oct 08 '13 at 06:03

4 Answers4

11

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.

Michael Oliver
  • 1,392
  • 8
  • 22
4

C++ doesn't support VLA (variable length array) so you should not use ListOfStudents[NumberOfStudents]. Use a const instead

edit:

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

this error says that there is no operator>> matched for type &int. you may want to loop and print each element of the array.

tristan
  • 4,235
  • 2
  • 21
  • 45
1

You can do it like this, by creating a local variable in your function (since global variables aren't though recommended):

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;
}

Or if you want the global variable, change the name of it for the compiler to make difference between the structure and the variable name, and then use it in your main() function with myClassRoom, instead of classroom:

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

EDIT: You have the set the length for the ListOfStudents to be a fixed number, for example

int ListOfStudents[512];

And then, this code compiles in my 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;
}
Victor
  • 13,914
  • 19
  • 78
  • 147
0

You can't have a variable of the same name as a struct, change the "classroom" at the bottom of the struct to something else. Or make the Struct "Classroom".

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

You could also make a variable of type struct in main using:

classroom someClassroom;
  • 2
    This is not the issue. It isn't a good idea for the instance name to mask the class name, but the code should compile. The problem really is the VLA. – juanchopanza Oct 08 '13 at 06:05