String array as object params

So, basically, I have a Question object with the constructor
 
Question(string question, string answer1, string answer2, string answer3, string answer4, int correct)

I call the Question objects by doing:
 
Question("question", "option1"... "option4", 1),

Obviously this is not efficient whatsoever. So, i attempted to do:
 
Question(string question, string answersArray[4], int correct)

which works, but when I go to coding the trivia questions (it's a trivia game)
I do this but I get an error
 
Question("example question", {"option1", "option2", "option3", "option4"}, 1),


It's not a run time error it's an error on calling
1
2
3
Question questions[10] = {
Question("", {"answers"}, number)
}


The error says: no instance of constructor "Question::Question" matches the argument list
Last edited on
So what happends if you change {" "," "..} to closed brackets instead [" ", " "]
that does not work, unfortunately.
closed account (E0p9LyTq)
Have another Question constructor, an overloaded constructor, that takes 3 parameters. 2 strings and an int. Your only declared constructor takes 6. 5 strings and an int.

See "overloading constructors" here:
http://www.cplusplus.com/doc/tutorial/classes/
1
2
3
4
5
6
7
8
9
10
11
12
13
Question();
Question(param,param,param,param,param,param);
Question(String q, String a[], int c);

stuff 
stuff
stuff

String q = "q";
String wat[2] = {"wat1","wat2"};
int c = 666;

Question disMyQuestion = new Question(q,wat,c);
Last edited on
I have 3 constructors, a default constructor and an overloaded constructor that takes a string, a string array and an int. I also have another constructor, like you suggested, that takes 2 strings and an int. I still get errors.
What exactly are the errors?

Ah. So my error was because I was instantiating it as a vector rather than an array of strings.
 
Question::Question(string q, vector<string> array, int correct)

did the trick!
Last edited on
Topic archived. No new replies allowed.