简体   繁体   中英

How do I create an array of this struct in c++ 11?

I am new to c++ and I want to create and array of the below struct. Any help please! thanks

struct contact{
    string name;
    string address;
    string phone;
    string email;
    contact(string n, string a, string p, string e);
    };

The problem seems to be that you are attempting to instantiate an array of contact objects, but this class has no default constructor, because you have added a non-default user defined constructor. This removes the compiler-generated default constructor. To get it back, you can use default :

struct contact{
  string name;
  string address;
  string phone;
  string email;
  contact() = default; // HERE
  contact(string n, string a, string p, string e);
};

This allows you to do this kind of thing:

contact contactsA[42];
std::array<contacts, 42> contactsB;

Edit : An alternative solution, given the simplicity of your type, is to remove the user defined constructors. This would make the type an aggregate, which would allow you to use aggregate initialization, and you would have to take no special action to enable default construction:

struct contact
{
  string name;
  string address;
  string phone;
  string email;
};

Now you can use aggregate initialization:

contact c{"John", "Doe", "0123-456-78-90", "j.doe@yoyodyne.com"};

and instantiate arrays as before:

contact contactsA[42];
std::array<contacts, 42> contactsB;

In C++, if you create a class without any constructors, the compiler will create a trivial default constructor for you - that is, a constructor that takes no arguments. Since you've created a non-default constructor, the compiler will not generate a default constructor for you. You would typically create an array of type "contact" with the following:

contact my_array[10];

This would call the contact's default constructor on each member. Since there is no default constructor, you will likely see this fail to compile.

I would recommend adding a default constructor. Your new struct might look something like the following:

struct contact{
  string name;
  string address;
  string phone;
  string email;
  contact();  // This is your default constructor
  contact(string n, string a, string p, string e);
};

After you do that, you should now be able to create an array with the following:

contact my_array[10];
#include <vector>
#include <array>
#include <string>

using std::string;

struct contact{
  string name;
  string address;
  string phone;
  string email;
  contact(string n, string a, string p, string e);
};

std::vector<contact> contacts1;      // for an array without a size known at compile time.
std::array<contact, 1> contacts2 = { // for an array with a known and static size.
  contact{ "Bob Smith", "42 Emesgate Lane, Silverdale, Carnforth, Lancashire LA5 0RF, UK", "01254 453873", "bob@example.com"}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM