0

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);
    };
1
  • 5
    Such a basic question shouldn't require a stackoverflow question; Whatever book or tutorial you're learning C++ from should cover this, or a google search for 'c++ array'. Stackoverflow shouldn't be your first and only resource. Commented Oct 13, 2013 at 15:51

3 Answers 3

3

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", "[email protected]"};

and instantiate arrays as before:

contact contactsA[42];
std::array<contacts, 42> contactsB;
Sign up to request clarification or add additional context in comments.

3 Comments

Or just leave out both ctors and rely on aggregate initialization?
@DyP good point, I hadn't noticed the one-to-one mapping between contsructor parameters and data members. I edit my answer accordingly.
It's not only the 1-to-1 mapping in the ctor; what struck me are the four public non-const data members. Whatever the ctor does, a free function can do the same (provided it has any way to create an instance).
1

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

2 Comments

This was very helpful! thank you :) Is there a way for me to create an array with a variable? For example, I want to create an array that is the size of the number of lines in a file. So I am getting the number of lines in a file, then storing it in a variable.
Thanks, don't forget to upvote and accept if it answered your question. Also make sure to take a look at @juanchopanza 's answer; it provides even more information. To create an array of variable length, I'd recommend a tutorial, such as this one
0
#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", "[email protected]"}
};

5 Comments

the std::array version will probably barf because contact does not have a default constructor.
yes you're right the std::array did not work I tried searching for how to create a default constructor and had no luck
@user2843336 you don't have to do much. Just contact() {} (or contact()=default if you have C++11).
@user2843336 that should go inside your class definition.
There, I've changed the code so that it will compile (and doesn't require a default constructor for contract).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.