Borrowing values from a member variable?

2019-08-15 17:19发布

问题:

I've sat here for 10 minutes trying to think of the title. If anyone can, feel free to change it.

Anyways, I have two classes campus and building as well as two arrays in campus itself. However those arrays are what I have to use to set the value of ID in building anyways.

In campus, I have loadFile and searchList

loadFile reads from a file, and sets values to three member variables in building and the two arrays mentioned earlier.

However I need to use searchList to print out those values. (It's an assignment. If I had it my way I'd probably do it differently cause I'm frankly bad at this).

Here's where it all goes downhill. I'm confused with how to proceed. The first thing I tried to do was call the constructor for building in loadFile which is part of campus using building::building(ID, name, description) which is supposed to set those values in the constructor itself.

Then in searchList, I thought I could go cout << building::getID() << building::getName() << building::getDescription(); (all of these are member functions in building) but obviously through a series of errors I realized this isn't the way to go.

So I tried instead of using the scope resolution operator, I should make a variable of type building somewhere. So I made building a inside loadFile and set a = building(ID, name, description) after defining those values.

Of course, this way I can't access a.getID in searchList. So I tried making it a global variable but obviously it wouldn't be that simple.

If you need more context, here's the assignment details.

Here's my class header

int const maxPerBuildingType = 7;
class campus
{
friend class building;
//friend building::building(int, std::string, std::string);


private:
//int academic[maxPerBuildingType]; removed
//int recreation[maxPerBuildingType]; removed
building recreation[maxPerBuildingType];
building academic[maxPerBuildingType]; 
int numberOfAcademic; 
int numberOfRecreation;
std::string temp;
protected:

public:

campus();
static int const maxPerBuildingType;
void loadFile();
void searchList();

};

I don't know if you need the definitions since it's wrong anyways but it's here (updated for edit) if you need it. I'll edit it into this post if necessary.

Anyways guys, thanks.

EDIT: Of course, the answer was because I was stupid. Arrays had to be of type building so i could allocate the required number of buildings. I haven't completed the assignment but for now I believe everything's fine. That'll probably change but eh. Thank you guys!

回答1:

AT the moment you are not storing your buildings anywhere. You put them into a local variable called a then throw them away.

I suspect the intention of the exercise is to store the buildings into the arrays academic and recreation. You currently have them as int arrays, but possibly they should be arrays of the specific building classes (I assume the exercise also wants you to have different building classes for the types of buildings, probably as a subclass of Building).

Once your campus has its own lists of buildings, at lot of your questions will answer themselves naturally.