Is it possible to have an array as a class member? It is part of my Wumpus Hunt exercise.

I want to do the following:

class Room {
   blah blah;
};

int main() {

for (i = 0; i < 3; i++) {
   Room adjacent[i];
      blah blah;
}

My compiler is not letting me do that. It warns that the variable must be constant.

Thanks in advance.

Recommended Answers

All 3 Replies

i've never heard of a function adjacent... but this is how i go through my array of class

cout<<"Enter the number of faculty you currently have: ";
    cin>>index;
    
    for(int i=0;i<index;i++)
    {
    coscFaculty[numcoscFaculty].loadcoscFaculty(coscFaculty,numcoscFaculty);
    numcoscFaculty++;
    }

this is my function

void facultyType::loadcoscFaculty(facultyType coscFaculty[], int numcoscFaculty)
{
infile>>fName>>lName>>Dept>>salary>>years;
}

you are gonna have to be more specific if i can help you more

Yes it is possible to have an array as a class member. However, in all arrays in C++, the number of elements must be a compile-time constant. In the code you cited, you tried to define an array with i elements, where i is a variable that changes at run time. C++ doesn't allow this.

I suggest you use std::vector<Room> instead.

This thread is continued in one titled Wumpus Hunt 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.