从成员变量借用值? [英] Borrowing values from a member variable?

查看:126
本文介绍了从成员变量借用值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里坐了10分钟想想标题。

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

无论如何,我有两个课程 campus building 以及 campus 本身中的两个数组。然而,这些数组是我必须使用设置 ID 建立的价值。

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.

校园中,我有 loadFile searchList

loadFile 从文件读取,

但是我需要使用 searchList 打印出这些值。 (这是一个任务,如果我有它的方式,我可能会做不同,因为我坦率地说,在这个)。

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).

这里是所有下坡。我很困惑如何继续。我试图做的第一件事是调用中的构造函数 c> loadFile >校园使用 building :: building(ID,name,description),它应该在构造函数中设置这些值。

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.

然后在 searchList 中,我想我可以去 cout< building :: getID()<< building :: getName()<< build :: getDescription(); (所有这些都是构建中的成员函数),但是显然通过一系列错误,我意识到这不是

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.

所以我尝试了而不是使用范围解析操作符,我应该创建一个类型的变量某处。所以我使里面构建了一个 c> loadFile 并设置了 a = building(ID,name,description )

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.

当然,这样我不能访问 a.getID searchList 中。所以我试图使它成为一个全局变量,但显然不会那么简单。

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.

这是我的类头

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();

};



我不知道你是否需要定义,因为它是错误的,但它这里(更新为编辑)。

编辑:当然,如果需要,我将把它编辑成这篇文章。

Anyways guys, thanks.

,答案是因为我很蠢。数组必须是类型建筑,所以我可以分配所需的建筑物数量。我没有完成任务,但现在我相信一切都很好。这可能会改变,但是。谢谢你们!

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!

推荐答案

在你不在任何地方存储你的建筑物的那一刻。你把它们放入一个局部变量 a ,然后把它们丢掉。

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

我怀疑练习的目的是将建筑物存储到数组 academic recreation 中。你目前有他们作为 int 数组,但可能他们应该是特定的建筑类的数组(我假设这个练习也希望你有不同的建筑类型的建筑物,可能是 Building 的子类。

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.

这篇关于从成员变量借用值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆