如何在函数参数中使用类
请帮助,我正在设计一本通讯录作为一个项目,我正在正确编译代码,但我遇到了错误。与 Address 类相关的问题..请随意复制并运行代码以查看我在说什么。提前致谢
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是通过 clang 运行时代码的输出(因为它的消息要好得多)。
上述意味着您不能执行此操作:
address = ""
,因为您没有从const char*
到Address< 的任何隐式转换/代码> 对象。
您的意思可能是
this->address = address
,因为您似乎想要分配在构造函数中收到的address
?附带说明一下,根据您使用的编译器,您可能希望通过引用传递
Address 地址
,例如Address&地址
或const Address&地址
(表示您不会修改所引用的对象)在函数参数列表中。尽管某些编译器(如果不是最广泛使用的编译器)将实现 复制省略优化。例如,您的构造函数参数将如下所示:
您的成员对象称为
address
,而不是Address
。您想要调用address.output()
,否则Address.output()
实际上是在尝试调用静态
函数从Address
类调用output
。与上面的问题相同,请使用
address.output()
,因为您在address
上调用函数output()
。return address;
是返回address
对象的正确方法。return Address地址;
是废话。此处未使用
fn
。只是一个警告,但它表明您要么忘记使用它,要么可以将其从代码中删除而不会造成任何损害。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 anAddress
object.You probably meant
this->address = address
, since it seems like you'd want to assign theaddress
you received in the constructor?As a side note, depending on the compiler you use, you might want to pass
Address address
by reference likeAddress& address
orconst 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 astatic
function calledoutput
from theAddress
class.Same problem as above, use
address.output()
since you're calling the functionoutput()
onaddress
.return address;
is the correct way to return theaddress
object.return Address address;
is nonsense.fn
is unused here. Just a warning, but it indicates you either forgot to use it, or it can be removed from your code without harm.通过查看您的代码,错误似乎出现在以下位置(我还没有编译您的代码)
当您这样做时,
您还没有重载 = 运算符来将地址设置为空字符串。这是无效的。
您必须重载“=”运算符才能将 Address 类的每个成员设置为空字符串。
尝试这样的事情:
在你的 input() 函数中:
地址.input();
除非将函数设为静态,否则不能使用类直接调用函数。
你应该使用:
address.input();
相似地。而不是
使用
另一个错误一定在这里:
你不返回像这样的指针。
想象一下如何返回 char 指针。
例如,如果您有:
那么假设在函数中:
与您的代码类似,您应该返回对象而不是类类型。
IE
By seeing your code, the error seems to be at the following places (I haven't compiled your code)
When you do
You haven't overloaded = operator to set address to an empty string. It is invalid.
You must overload "=" operator to set each member of the Address class to an empty string.
Try something like this:
in your input() function:
Address.input();
you cannot use a class to invoke a function directly unless you make the function static.
You should use:
address.input();
similarly. instead of
use
Another error must be here:
You do not return pointers like this.
Just imagine how would you return a char pointer.
For example if you have:
then suppose in a function:
similarly in your code, you should return the object and not the class type.
i.e.