如何在函数参数中使用类

发布于 2024-12-11 23:19:24 字数 2737 浏览 0 评论 0原文

请帮助,我正在设计一本通讯录作为一个项目,我正在正确编译代码,但我遇到了错误。与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青巷忧颜 2024-12-18 23:19:24

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

λ > 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&地址const Address&地址(表示您不会修改所引用的对象)在函数参数列表中。尽管某些编译器(如果不是最广泛使用的编译器)将实现 复制省略优化

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

contact(string f_n, string l_n, const Address& home,string emaila, string num);

    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类调用output


blah.cxx:97:60: error: expected expression
cout<<"name: "<<fn<<" "<<ln<<" address "<<Address.output();<<" email: "<<email<<"         digits "<<number<<endl;

与上面的问题相同,请使用 address.output(),因为您在 address 上调用函数 output()


blah.cxx:110:8: error: 'Address' does not refer to a value
return Address address;
       ^

return address; 是返回 address 对象的正确方法。 return Address地址;是废话。


blah.cxx:113:8: warning: expression result unused [-Wunused-value]
return fn, ln;
       ^~
1 warning and 5 errors generated.

此处未使用 fn。只是一个警告,但它表明您要么忘记使用它,要么可以将其从代码中删除而不会造成任何损害。

Here's the output of your code when run through clang (for its far nicer messages).

λ > 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

The above means you can't do this: address = "", because you don't have any implicit conversions from a const char* to an Address object.

You probably meant this->address = address, since it seems like you'd want to assign the address you received in the constructor?

As a side note, depending on the compiler you use, you might want to pass Address address by reference like Address& address or const 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:

contact(string f_n, string l_n, const Address& home,string emaila, string num);

    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;
                                          ^

Your member object is called address, not Address. You want to call address.output(), otherwise Address.output() is really trying to call a static function called output from the Address class.


blah.cxx:97:60: error: expected expression
cout<<"name: "<<fn<<" "<<ln<<" address "<<Address.output();<<" email: "<<email<<"         digits "<<number<<endl;

Same problem as above, use address.output() since you're calling the function output() on address.


blah.cxx:110:8: error: 'Address' does not refer to a value
return Address address;
       ^

return address; is the correct way to return the address object. return Address address; is nonsense.


blah.cxx:113:8: warning: expression result unused [-Wunused-value]
return fn, ln;
       ^~
1 warning and 5 errors generated.

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.

溺孤伤于心 2024-12-18 23:19:24

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

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;
}

当您这样做时,

address ="";

您还没有重载 = 运算符来将地址设置为空字符串。这是无效的。

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

尝试这样的事情:

Address operator=(string str)
{
  this.home = str;
  this.street = str;
  this.apt = str;
  this.city = str;
  this.state = str;
  this.zip = str;
}

在你的 input() 函数中:
地址.input();

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

address.input();

相似地。而不是

Address.output();

使用

address.output();

另一个错误一定在这里:

return Address address;

你不返回像这样的指针。
想象一下如何返回 char 指针。
例如,如果您有:

char *a;

那么假设在函数中:

(char*) test()
{
  return a; //notice not "return char a"
}

与您的代码类似,您应该返回对象而不是类类型。
IE

return address; //not return Address address

By seeing your code, the error seems to be at the following places (I haven't compiled your code)

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;
}

When you do

address ="";

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:

Address operator=(string str)
{
  this.home = str;
  this.street = str;
  this.apt = str;
  this.city = str;
  this.state = str;
  this.zip = str;
}

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

Address.output();

use

address.output();

Another error must be here:

return Address address;

You do not return pointers like this.
Just imagine how would you return a char pointer.
For example if you have:

char *a;

then suppose in a function:

(char*) test()
{
  return a; //notice not "return char a"
}

similarly in your code, you should return the object and not the class type.
i.e.

return address; //not return Address address
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文