首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在函数参数中使用类

如何在函数参数中使用类
EN

Stack Overflow用户
提问于 2011-10-26 11:45:44
回答 2查看 1.8K关注 0票数 0

请帮助,我设计了一个通讯录作为一个项目,我有编译代码正确和我得到错误。与Address类相关的问题..你可以随意复制并运行代码,看看我在说什么。提前感谢

代码语言:javascript
复制
 #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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-10-26 12:10:13

下面是通过clang运行代码时的输出(因为它的消息要好得多)。

代码语言:javascript
复制
λ > clang++ blah.cxx 
blah.cxx:81:27: error: no viable overloaded '='
fn= f_n; ln= l_n; address ="" ;email= emaila;number= num;
                  ~~~~~~~ ^~~
blah.cxx:5:11: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from 'const char [1]' to 'const Address' for 1st argument

上面的代码意味着您不能这样做:address = "",因为您没有任何从const char*Address对象的隐式转换。

您可能指的是this->address = address,因为您似乎想要分配在构造函数中收到的address

另外,根据您使用的编译器,您可能希望在函数参数列表中通过引用传递Address address,如Address& addressconst Address& address (表示不会修改被引用的对象)。尽管一些编译器(如果不是使用最广泛的编译器)将实现copy ellision optimization

例如,您的构造函数参数将如下所示:

代码语言:javascript
复制
contact(string f_n, string l_n, const Address& home,string emaila, string num);

代码语言:javascript
复制
    class Address
          ^
blah.cxx:89:8: error: expected unqualified-id
Address.input();
       ^
blah.cxx:97:43: error: 'Address' does not refer to a value
cout<<"name: "<<fn<<" "<<ln<<" address "<<Address.output();<<" email: "<<email<<"         digits "<<number<<endl;
                                          ^

您的成员对象名为address,而不是Address。您希望调用address.output(),否则Address.output()实际上是在尝试从Address类调用一个名为outputstatic function

代码语言:javascript
复制
blah.cxx:97:60: error: expected expression
cout<<"name: "<<fn<<" "<<ln<<" address "<<Address.output();<<" email: "<<email<<"         digits "<<number<<endl;

由于您在address上调用函数output(),因此请使用address.output()

代码语言:javascript
复制
blah.cxx:110:8: error: 'Address' does not refer to a value
return Address address;
       ^

return address;是返回address对象的正确方式。return Address address;是胡说八道。

代码语言:javascript
复制
blah.cxx:113:8: warning: expression result unused [-Wunused-value]
return fn, ln;
       ^~
1 warning and 5 errors generated.

这里没有使用fn。这只是一个警告,但它表明您要么忘记使用它,要么可以将其从您的代码中删除而不会造成损害。

票数 2
EN

Stack Overflow用户

发布于 2011-10-26 12:00:54

通过查看您的代码,错误似乎出现在以下位置(我没有编译您的代码)

代码语言:javascript
复制
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;
}

当你这样做的时候

代码语言:javascript
复制
address ="";

您尚未重载=运算符以将地址设置为空字符串。无效。

必须重载"=“运算符,才能将Address类的每个成员设置为空字符串。

尝试如下所示:

代码语言:javascript
复制
Address operator=(string str)
{
  this.home = str;
  this.street = str;
  this.apt = str;
  this.city = str;
  this.state = str;
  this.zip = str;
}

在输入()函数中: Address.input();

除非将函数设为静态,否则不能使用类直接调用函数。您应该使用:

address.input();

类似的。而不是

代码语言:javascript
复制
Address.output();

使用

代码语言:javascript
复制
address.output();

这里肯定有另一个错误:

代码语言:javascript
复制
return Address address;

您不会像这样返回指针。想象一下,你将如何返回一个字符指针。例如,如果您有:

代码语言:javascript
复制
char *a;

然后假设在一个函数中:

代码语言:javascript
复制
(char*) test()
{
  return a; //notice not "return char a"
}

同样,在您的代码中,您应该返回对象,而不是类类型。即

代码语言:javascript
复制
return address; //not return Address address
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7898524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档