1

I'm trying to initialize an array of structs, but the structs demand arguments upon initialization, like this:

#include <iostream>
using namespace std;

struct MyStruct{
    string par1;
    double par2;
    MyStruct(string var1, double var2){
        par1=var1;
        par2=var2;
    }
};

int main(){
    MyStruct test("apples", 17.5);
    MyStruct MyArray[3];
return 0;
}

the structure 'test' initializes without problem, but when MyArray tries to initialize, it gives me

'no matching function for call to MyStruct::MyStruct()'

and also

'candidate expects 2 arguments, 0 provided'

I assume this is because I have to initialize it using some arguments for par1 and par2, but I don't know how to, or if it's even possible. I would appreciate any help, thank you.

1
  • 1
    Vector allows you to construct one object and copy-construct it to make a bunch of them. If you wish all objects to be initialized differently, you can use a loop to construct one by one and push_back. Commented Apr 10, 2015 at 15:05

3 Answers 3

4

Provide an initialiser for each array element:

MyStruct MyArray[3] = {
    MyStruct("oranges", 42.6),
    MyStruct("bananas", 63.2),
    MyStruct("pomegranates", 3.2)
};

Since C++11, this can be reduced slightly:

MyStruct MyArray[3] {
    {"oranges", 42.6},
    {"bananas", 63.2},
    {"pomegranates", 3.2}
};
Sign up to request clarification or add additional context in comments.

4 Comments

Ah, so I was just missing the syntax. How do I initialize the whole array to a default value such as MyStruct("nil", 0) and add arguments later? Whenever I try that I get "error: could not convert '<brace-enclosed initializer list>()' from '<brace-enclosed initializer list>' to 'MyStruct'"
@Simeon: You don't. You either initialise them all individually, or provide a default constructor to value-initialise ones with missing initialisers. Or use std::vector rather than an array.
How do I provide a constructor that only initializes the ones with missing arguments/initialisers?
@Simeon: By declaring and defining a default constructor in the usual manner. You can't constrain it only to be used in an array with missing initialisers, so if you don't want it to be default-constructible in general, you'll have to use one of the other suggestions.
1
int main(){
    std::vector<MyStruct> MyArray(3, MyStruct("apples", 17.5));
return 0;
}

This method has some limitations: 1)elements in the vector are the same, 2)class has to be copy-constructable.

2 Comments

This is exactly what I wanted, since my array has to be initialized very long and then modified afterwards. Is there a way to do it without using the vector library?
@Simeon, unless you re-write MyStruct::MyStruct(void). But I really don't think there is a reason to avoid std::vector, for you can do &MyArray[0] to "down grade" it to a raw pointer/array.
0

Is there any reason against the use of std::vector?

#include <vector>

std::vector< MyStruct > mystructs;

mystructs.push_back( MyStruct( "apples",   17.5 ) );
mystructs.push_back( MyStruct( "bananas",   7.5 ) );
mystructs.push_back( MyStruct( "cherries", 17.5 ) );

5 Comments

Not my downvote, but there's no good reason to add unnecessary memory allocations when all that's needed is a simple automatic array.
@MikeSeymour You don't always know at initialization time what the value of the objects will be. You will ultimately need dynamic allocation for non default-constructible objects which std::vector facilitates.
Vector can copy-construct one object into an array/vector, hence easier to use if number of objects is large and objects don't need to be initialized with different parameters.
@MikeSeymour, 0x499602D2, user3528438, thanks for your comments. I agree with all three of them. It probably depends on the specific case which approach is more suitable.
I'd say this ignores the OP's question and talks about something else, and the OP sounded neither confused nor insane.

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.