You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, September 27th 2016, 7:40am

private vector of a class

hi, i try to use a private vector to get a list of contact. however, it always tell me that it is a private member of a class. which cannot get used by an other class. I tried to use the "Friend" function. but it still doesn't work. here is all my code.
ContactList.h

Source code

1
#ifndef CONTACTLIST_H#define CONTACTLIST_H#include "Contact.h"using namespace std;class ContactList{		public:		 ContactList(Contact);		private:			 string fname; 		string lname;		 string phone;			 string address;			 string city;			 string state; 		 vector <Contact> myList;};#endif


Contact.h

Source code

1
#ifndef CONTACT_H#define CONTACT_H#include <iostream>#include <vector>using namespace std;class Contact{	 public: 	 //Constructors		 Contact(const string& fname, const string& lname, const string& phone, const string& address, const string& city, const string& state);		 Contact(const string& fname, const string& lname);	 //Functions			 void addContact();		 //Accessor			 void setFirstName(string fname);		 void setLastName(string lname);			 void setPhone(string phone);		 void setAddress(string address);			 void setCity(string city);			 void setState(string state);			 string getFirstName();			string getLastName();			 string getPhone();			 string getAddress();			 string getCity();			 string getState();		 //ostream Operator		 friend ostream& operator<<(ostream& out, Contact& str);		 friend class ContactList;	 private:	//Variables			 string fname;	string lname;	string phone;	string address;	string city;	string state;};#endif


Contact.cpp

Source code

1
2
3
4
5
6
#include "ContactList.h"#include <iostream>#include <fstream>#include <string>
using namespace std;
	Contact::Contact(const string& myFname, const string& myLname, const string& myPhone, const string& myAddress, const string& myCity, const string& myState){		 fname = myFname;		 lname = myLname;			phone = myPhone;		 address = myAddress;		 city = myCity;		 state = myState; }

void Contact::addContact(){		 string fn, ln, ph, ad, ci, st;		cout<<"\tFirst Name :> ";				 getline(cin,fn);		 cout<<"\tLast Name :> ";			 getline(cin,ln);			 cout<<"\tPhone # :> ";				 getline(cin,ph);			 cout<<"\tAddress :> ";				 getline(cin,ad);			 cout<<"\tcity :> ";				 getline(cin,ci);			 cout<<"\tstate :> ";				 getline(cin,st);			 fname	= fn;			 lname	= ln;			 phone	= ph;			 address	= ad;			 city	= ci;		 state	= st;		 ContactList::myList.push_back(Contact::Contact(fname,lname,phone,address,city,state));}//ostream operator	ostream& operator<<(ostream& out, Contact& str){		 out<<str.getFirstName()<<" "<<str.getLastName()<<"\tPhone Number: "<<str.getPhone()<<endl;			 out<<str.getAddress()<<endl;			 out<<str.getCity()<<", "<<str.getState()<<endl;	 return out;}
//Accessor Setters	void Contact::setFirstName(string sFname){				Contact::fname = sFname;	}	void Contact::setLastName(string sLname){			Contact::lname = sLname;	}	void Contact::setPhone(string sPhone){			Contact::phone = sPhone;	}	void Contact::setAddress(string sAddress){			Contact::address = sAddress;	}	void Contact::setCity(string sCity){			Contact::city = sCity;	}	void Contact::setState(string sState){			Contact::state = sState;	}	//Accessor Getters	string Contact::getFirstName(){	return Contact::fname;}	string Contact::getLastName(){	return Contact::lname;}	string Contact::getPhone(){	return phone;}	string Contact::getAddress(){	return address;}	string Contact::getCity(){	return city;}	string Contact::getState(){	return state;}


The error message i get is :

Source code

1
2
Contact.cpp:47:17: error: invalid use of non-static data member 'myList ContactList :: myList.push_back(Contact::Contact(fname,lname,phone,address,city,state));
Contact.cpp: 47:17: error: 'myList' is a private member of 'ContactList'./ContactList.h:26:20: declared private here: vector <Contact> myList;

This post has been edited 2 times, last edit by "bozmin" (Sep 27th 2016, 7:49am)


Similar threads