Hi guys, I'm still new to C++ and get confused by the codes below..

class FLATMATE
{
	string name;
	public: 
		FLATMATE() { name = "";}
		FLATMATE(string n){name = n;}
		string getName(){return name;}
		void setResident(string n){name = n;}
};
class DUTY
{
	FLATMATE resident;		//name of the flatmate
	int RequestOffer;		//amount of dollars used to do request
	FLATMATE AcceptedBy;	//acceptee
	public:
	DUTY(FLATMATE f){resident = f;}
	FLATMATE & getResident(){return resident;}
	void setRequestOffer(int f){RequestOffer = f;}
	int getRequestOffer(){return RequestOffer;}
	void setAcceptedBy(FLATMATE b){AcceptedBy = b;}
	FLATMATE getAcceptedBy(){return AcceptedBy;}
	
};

Above are the classes and when the function calls the class method, it is a bit weird...

after storing

void DUTYLISTAPP::WashList(string filename)
{
	
	ifstream myfile("FlatData1.dat");
	int day; 	//2 weeks, 0-13 days
	string RequesterName;
	int RequestOffer;
	int RequestToDate;
	string Accepter;
	WASH *WashPtr;
	FLATMATE *residentptr, *residentptr1;
	
	if(!myfile)
	{
		cout<<"can't open file "<<endl;
		exit(1);
	}
	while(true)
	{
		myfile>>day;
		if(myfile.eof()) break;
		myfile >> RequesterName;
		residentptr = new FLATMATE(RequesterName);
		WashPtr = new WASH(*residentptr, day);
		myfile>>RequestOffer;
		WashPtr->setRequestOffer(RequestOffer);
		myfile>>RequestToDate;
		WashPtr->setDayTo(RequestToDate);
		myfile>>Accepter;
		residentptr1 = new FLATMATE(Accepter);
		WashPtr->setAcceptedBy(*residentptr1);
		WashDutyList.push_back(*WashPtr);
	}
	
}

we call the method

void DUTYLISTAPP::ShowRoster()
{
	for(int j =0; j<WashDutyList.size();j++)
	{
		cout<<(WashDutyList.at(j)).getDayFrom()<<"\t";
		cout<<(WashDutyList.at(j)).getResident().getName()<<"\t";
		cout<<(WashDutyList.at(j)).getRequestOffer()<<"\t";
		cout<<(WashDutyList.at(j)).getDayTo()<<"\t";
		cout<<(WashDutyList.at(j)).getAcceptedBy().getName()<<endl;
	}
}

I just don't understand why we need to use WashDutyList.at(j)).getResident().getName() to get the name, i thought every object is stored in vector so we can just use WashDutyList.at(j)).getName() to get the name? Any advice is greatly appreciated!

Your DUTY class does not have a getName() function, hence you have to first get the instance of a class (a FLATMATE) which does have a getName() function (WashDutyList.at(j)).getResident()) and then call getName() on it.

Thank you, it helped a lot :)

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.