7

I have this class constructor:

Pairs (int Pos, char *Pre, char *Post, bool Attach = true);

How can I initialize array of Pairs classes? I tried:

Pairs Holder[3] =
{
    {Input.find("as"), "Pre", "Post"},
    {Input.find("as"), "Pre", "Post"},
    {Input.find("as"), "Pre", "Post"}
};

Apparently it's not working, I also tried to use () brackets instead of {} but compiler keeps moaning all the time. Sorry if it is lame question, I googled quite hard but wasn't able to find answer :/

1
  • You will usually get faster and better replies when posting the error messages from the compiler. Commented Jun 6, 2010 at 10:32

2 Answers 2

14

Call the constructor explicitly:

Pairs Holder[3] =
{
    Pairs(Input.find("as"), "Pre", "Post"),
    Pairs(Input.find("as"), "Pre", "Post"),
    Pairs(Input.find("as"), "Pre", "Post")
};
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand the syntax. I thought you can't call the constructor explicitly (it is implicitly called when an object is instantiated). Additionally, it does not return anything.
6

Call the constructor:

Pairs Holder[3] =
{
    Pairs(Input.find("as"), "Pre", "Post"),
    Pairs(Input.find("as"), "Pre", "Post"),
    Pairs(Input.find("as"), "Pre", "Post")
};

This is similar to saying

Holder[0] = Pairs(Input.find("as"), "Pre", "Post");
Holder[1] = Pairs(Input.find("as"), "Pre", "Post");
Holder[2] = Pairs(Input.find("as"), "Pre", "Post");

A full-fledged class can be found here.

1 Comment

Thanks for the link, tutorial there seems to be interesting, I'll give it a go. I started to believe I have some error within my code when your first (unedited) comment caused compiler errors as well. Luckily this wasn't the case, thanks for editing.

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.