在 C++ 中创建一个类对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5674771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:39:17  来源:igfitidea点击:

create an array of class objects in c++

c++

提问by Frustrated Coder

Hi guys I want to make an array of class objects....so that I can keep on creating as many objects during runtime as and when required I wrote the following code, but its giving me error:

嗨,伙计们,我想创建一个类对象数组....这样我就可以在运行时根据需要继续创建尽可能多的对象,我编写了以下代码,但它给了我错误:

class contact{
public:
    string name;//ALL CLASS VARIABLES ARE PUBLIC
    int phonenumber;
    string address;

    contact(){//Constructor
        name= NULL;
        phonenumber= NULL;
        address= NULL;
    }

    void input_contact_name(string s){//function to take contact name
        name=s;
    }
    void input_contact_number(int x){//function to take contact number
        phonenumber=x;
    }
    void input_contact_address(string add){//function to take contact address
        address=add;
    }
}

int main(){
    contact *d;
    d= new contact[200];
    string name,add;
    int choice;//Variable for switch statement
    int phno;
    static int i=0;//i is declared as a static int variable
    bool flag=false;
    cout<<"\tWelcome to the phone Directory\n";//Welcome Message
    cout<<"Select :\n1.Add New Contact\n2.Update Existing Contact\n3.Delete an Existing Entry\n4.Display All Contacts\n5.Search for a contact\n6.Exit PhoneBook\n\n\n";//Display all options
    cin>>choice;//Input Choice from user
    while(!flag){//While Loop Starts
        switch(choice){//Switch Loop Starts
        case 1:
            cout<<"\nEnter The Name\n";
            cin>>name;
            d[i]->name=name;
            cout<<"\nEnter the Phone Number\n";
            cin>>phno;
            d[i]->input_contact_number(phno);
            cout<<"\nEnter the address\n";
            cin>>add;
            d[i]->input_contact_address(add);
            i++;
            flag=true;
        }
    }
    return 0;
}

Please can some one out figure out the reason?? Thanks in advance

请问有大佬能查一下原因吗??提前致谢

回答by ildjarn

Manyproblems:

很多问题:

  1. Missing semicolon on the closing brace of the class, as maverik noted
  2. Use of stringwithout using namespace std;or using std::string;(or #include <string>for that matter)
  3. Ditto #2 for cinand cout(and <iostream>)
  4. d[i]->is wrong; d[i]is a contact&, not a contact*, so use .instead of ->
  5. name= NULL;and address= NULL;are nonsensical -- stringis not a pointer type, and already default-constructs to empty
  6. phonenumber= NULL;is technicallyvalid, but still 'wrong'
  1. 正如 maverik 指出的那样,类的右大括号上缺少分号
  2. 使用string没有using namespace std;using std::string;(或#include <string>就此而言)
  3. 同上 #2cincout(和<iostream>
  4. d[i]->是错的; d[i]是 a contact&,不是 a contact*,所以用.代替->
  5. name= NULL;并且address= NULL;是无意义的——string不是指针类型,并且已经默认构造为空
  6. phonenumber= NULL;技术上有效的,但仍然是“错误的”

Also, good lord, use some whitespace in your code.

另外,老天爷,在您的代码中使用一些空格。

EDIT(in response to comment): Your constructor should look like:

编辑(回应评论):您的构造函数应如下所示:

contact() : phonenumber() { }

回答by maverik

You forget the ;

你忘记了 ;

class contact {
    ...
};  // <- ; is neccessary