Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare an array with a custom class?

I'm trying to declare an array with a custom class. When I added a constructor to the class, my compiler complains that there's "No matching constructor for initialization of name[3]".

Here's my program:

#include <iostream>

using namespace std;

class name {
  public:
    string first;
    string last;

  name(string a, string b){
    first = a;
    last = b;
  }
};

int main (int argc, const char * argv[])
{

  const int howManyNames = 3;

  name someName[howManyNames];

  return 0;
}

What can I do to make this run, and what am I doing wrong?

like image 494
Moshe Avatar asked Dec 20 '11 17:12

Moshe


People also ask

How do you declare an array of classes?

Syntax: Class_Name obj[ ]= new Class_Name[Array_Length]; For example, if you have a class Student, and we want to declare and instantiate an array of Student objects with two objects/object references then it will be written as: Student[ ] studentObjects = new Student[2];

How do you create a custom array?

In this article we will see that how we can create arrays of custom types. We can achieve it by using custom types. We all know that array is a reference type i.e. memory for an array is allocated in a heap. Sometimes we need to declare arrays of custom types rather than arrays of predefined types ( int or string etc).

How do you declare an array of classes in C++?

Syntax: ClassName ObjectName[number of objects]; The Array of Objects stores objects. An array of a class type is also known as an array of objects.

How do you initialize a custom array in Java?

We can declare and initialize arrays in Java by using a new operator with an array initializer. Here's the syntax: Type[] arr = new Type[] { comma separated values }; For example, the following code creates a primitive integer array of size 5 using a new operator and array initializer.


1 Answers

You have to provide a default constructor. While you're at it, fix your other constructor, too:

class Name
{
public:
  Name() { }
  Name(string const & f, string const & l) : first(f), last(l) { }
  //...
};

Alternatively, you have to provide the initializers:

Name arr[3] { { "John", "Doe" }, { "Jane", "Smith" }, { "", "" } };

The latter is conceptually preferable, because there's no reason that your class should have a notion of a "default" state. In that case, you simply have to provide an appropriate initializer for every element of the array.

Objects in C++ can never be in an ill-defined state; if you think about this, everything should become very clear.


An alternative is to use a dynamic container, though this is different from what you asked for:

std::vector<Name> arr;
arr.reserve(3);  // morally "an uninitialized array", though it really isn't

arr.emplace_back("John", "Doe");
arr.emplace_back("Jane", "Smith");
arr.emplace_back("", "");

std::vector<Name> brr { { "ab", "cd" }, { "de", "fg" } }; // yet another way
like image 184
Kerrek SB Avatar answered Oct 14 '22 12:10

Kerrek SB