please help, im designing a contact book as a project and i’m having compiling the code properly and im getting errors. problem in relation to Address class.. feel free to copy and run code to see what im talking about. thanks in advance
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class Address
{
private:
string home;
string street;
string apt;
string city;
string state;
string zip;
public:
Address();
string getHome() const;
string getStreet() const;
string getApt() const;
string getCity() const;
string getState() const;
string getZip() const;
void output() const;
void input();
};
class contact{
private:
string fn;
string ln;
Address address;
string email;
string number;
public:
void input();
void output();
void setname(string f_n, string l_n);
void setaddress(Address home);
void setemail(string emaila);
void setnumber(string num);
string getname();
string getAddress();
string getemail();
string getnumber();
contact();
contact(string f_n, string l_n, Address home,string emaila,string num);
};
void menu(string opt);
int main(){
string opt="";
contact c;
c.input();
menu(opt);
c.output();
cout<<"input up to 10 contacts, type quit to stop if less than 10: "<<endl;
return 0;
}
void menu(string opt){
cout<<"Choose(type) a Menu: search | display all(show) | exit'"<<endl;
cin>>opt;
if(opt=="search")cout<<"write a function that index"<<endl;
else if(opt=="show")cout<<"write a function that display all: "<<endl;
else if(opt=="exit")exit(0);
}
contact::contact(){
fn=""; ln=""; Address address; email=""; number="";
}
contact::contact(string f_n, string l_n, Address address,string emaila,string num){
fn= f_n; ln= l_n; address ="" ;email= emaila;number= num;
}
void contact::input(){
for (int i=1; i<=10;i++){//allow 10 contacts
cout<<"fn and ln separate by a space: ";
cin>>fn>>ln;
cout<<"address: ";
Address.input();
cout<<"email: ";
cin>>email;
cout<<"phone number: ";
cin>>number;
}
}
void contact::output(){
cout<<"name: "<<fn<<" "<<ln<<" address "<<Address.output();<<" email: "<<email<<" digits "<<number<<endl;
}
void contact::setname(string f_n, string l_n){
fn= f_n; ln= l_n;
}
void contact::setemail(string emaila){
email= emaila;
}
void contact::setnumber(string num){
number= num;
}
string contact::getAddress(){
return Address address;
}
string contact::getname(){
return fn, ln;
}
string contact::getemail(){
return email;
}
string contact::getnumber(){
return number;
}
Here’s the output of your code when run through clang (for its far nicer messages).
The above means you can’t do this:
address = "", because you don’t have any implicit conversions from aconst char*to anAddressobject.You probably meant
this->address = address, since it seems like you’d want to assign theaddressyou received in the constructor?As a side note, depending on the compiler you use, you might want to pass
Address addressby reference likeAddress& addressorconst Address& address(indicating you won’t modify the object being referenced) in your function argument list. Though some compilers (if not the most widely used ones) will implement the copy ellision optimization.For example your constructor arguments would look like this:
Your member object is called
address, notAddress. You want to calladdress.output(), otherwiseAddress.output()is really trying to call astaticfunction calledoutputfrom theAddressclass.Same problem as above, use
address.output()since you’re calling the functionoutput()onaddress.return address;is the correct way to return theaddressobject.return Address address;is nonsense.fnis unused here. Just a warning, but it indicates you either forgot to use it, or it can be removed from your code without harm.