简体   繁体   English

如何在c ++ 11中创建此结构的数组?

[英]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. 我是c ++的新手,我想创建以下结构的数组。 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. 问题似乎是您试图实例化contact对象数组,但是此类没有默认构造函数,因为您添加了非默认的用户定义的构造函数。 This removes the compiler-generated default constructor. 这将删除编译器生成的默认构造函数。 To get it back, you can use default : 要取回它,可以使用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. 在C ++中,如果创建没有任何构造函数的类,则编译器将为您创建一个简单的默认构造函数-即,不带参数的构造函数。 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”类型的数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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