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?
This question is related to
c++
arrays
constructor
Your class:
class name {
public:
string first;
string last;
name() { } //Default constructor.
name(string a, string b){
first = a;
last = b;
}
};
Has an explicit constructor that requires two string parameters. Classes with no constructor written explicitly get default constructors taking no parameters. Adding the explicit one stopped the compiler from generating that default constructor for you.
So, if you wish to make an array of uninitialized objects, add a default constructor to your class so the compiler knows how to create them without providing those two string parameters - see the commented line above.
You need a parameterless constructor to be able to create an instance of your class. Your current constructor requires two input string parameters.
Normally C++ implies having such a constructor (=default parameterless constructor) if there is no other constructor declared. By declaring your first constructor with two parameters you overwrite this default behaviour and now you have to declare this constructor explicitly.
Here is the working code:
#include <iostream>
#include <string> // <-- you need this if you want to use string type
using namespace std;
class name {
public:
string first;
string last;
name(string a, string b){
first = a;
last = b;
}
name () // <-- this is your explicit parameterless constructor
{}
};
int main (int argc, const char * argv[])
{
const int howManyNames = 3;
name someName[howManyNames];
return 0;
}
(BTW, you need to include to make the code compilable.)
An alternative way is to initialize your instances explicitly on declaration
name someName[howManyNames] = { {"Ivan", "The Terrible"}, {"Catherine", "The Great"} };
To default-initialize an array of T
s, T
must be default constructible. Normally the compiler gives you a default constructor for free. However, since you declared a constructor yourself, the compiler does not generate a default constructor.
Your options:
initialize all the elements of the array upon declaration (you can do this because name
is an aggregate);
name someName[4] = { { "Arthur", "Dent" },
{ "Ford", "Prefect" },
{ "Tricia", "McMillan" },
{ "Zaphod", "Beeblebrox" }
};
use a std::vector
instead, and only add element when you have them constructed.
you just need to add a default constructor to your class to look like this:
class name {
public:
string first;
string last;
name() {
}
name(string a, string b){
first = a;
last = b;
}
};
In order to create an array of objects, the objects need a constructor that doesn't take any paramters (that creates a default form of the object, eg. with both strings empty). This is what the error message means. The compiler automatically generates a constructor which creates an empty object unless there are any other constructors.
If it makes sense for the array elements to be created empty (in which case the members acquire their default values, in this case, empty strings), you should:
-Write an empty constructor:
class name {
public:
string first;
string last;
name() { }
name(string a, string b){
first = a;
last = b;
}
};
-Or, if you don't need it, remove the existing constructor.
If an "empty" version of your class makes no sense, there is no good solution to provide initialisation paramters to all the elements of the array at compile time. You can:
init()
function which does the real initialisationvector
, and on initialisation create the objects and insert them into the vector
, either using vector::insert
or a loop, and trust that not doing it at compile time doesn't matter.std::vector<std::string> v = { "xyzzy", "plugh", "abracadabra" };
`
Source: Stackoverflow.com