Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Struct invalid use of non-static data member [duplicate]

Tags:

c++

arrays

struct

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;
                  ^
like image 683
ssj3goku878 Avatar asked Sep 14 '25 02:09

ssj3goku878


1 Answers

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.

like image 108
Michael Oliver Avatar answered Sep 16 '25 18:09

Michael Oliver