Im writing a simple program that records the quiz scores of students. It starts with asking the user the number of students in the class. I use this number to make an object array of that size.
However, there seems to be an error. First, here is a snippet of my code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::flush;
using std::string;
class Quizdata{
private:
string name;
double total;
int number;
public:
Quizdata(int SIZE)
{
total = 0;
number = 0;
for(int i=0; i<SIZE; i++)
{
cout << "Enter in name of student " << i+1 << ": ";
cin >> name;
}
}
int add_quiz(int score)
{
total+=score;
number++;
cout << "total so far is: " << total << endl;
}
string get_name()
{
return name;
}
int get_total_score()
{
return total;
}
int get_average_score()
{
return total/number;
}
};
void printresults(Quizdata[], int); // Prototype function
int main()
{
int SIZE, quizzes, score;
cout << "Enter in number of students in class: ";
cin >> SIZE;
Quizdata student[SIZE](SIZE); // This is supposed to make student object
// array of size SIZE and pass SIZE
// to the constructor
When I compile this, I get the following error: variable sized object 'student' may not be initialized.
What am I doing wrong here? When I change the constructor to a default one (without any parameters and the for-loop inside removed) and remove the variable argument passed in main, then the code works fine.
Looks like you're trying to initialize a Quizdata 2D array object, which doesn't exist in your class as an explicit constructor. You've got one explicit constructor that takes an int parameter.
EDIT: Disregard. I mistook the brackets for parentheses.
Then how come my variable size array works when I make the constructor default and remove the argument of 'SIZE' passed in main?
If you remove the 'SIZE' variable passed to the array size then... well, you don't have an array.
If you meant from the constructor, then it shouldn't work then as you still have a variable sized array that is not dynamically allocated.
No, I still have an array of objects called 'student'. If I removed the 'SIZE' in parenthesis (i.e. the argument passed to the constructor), and the parameters in the constructor, it works fine. If I keep it, I get the errors.
Quizdata(int SIZE)
{
total = 0;
number = 0;
for(int i=0; i<SIZE; i++)
{
cout << "Enter in name of student " << i+1 << ": ";
cin >> name;
}
}
...
...
...
cout << "Enter in number of students in class: ";
cin >> SIZE;
Quizdata student[SIZE](SIZE);
verses, this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Quizdata()
{
total = 0;
number = 0;
cout << "Enter name of student: ";
cin >> name;
}
...
...
...
cout << "Enter in number of students in class: ";
cin >> SIZE;
Quizdata student[SIZE];
The SECOND works, the FIRST doesn't. I just want to know why the FIRST doesn't work. You said its because I have a variable sized array, but I have a variable sized object array in both cases, so this doesn't make sense to me.
Neither should work at all unless you're using a compiler that supports variable sized arrays. And even the you can't declare an array of objects without a default constructor like that.
Here are your options then:
1.) Array of pointers : Quizdata *pStudents[SIZE]
2.) Vector : std::vector<QuizData> students(SIZE, QuizData(SIZE));
What I have right now (object array) seems to be working and has always worked for me so I'll stick with that. You keep saying it shouldn't work but I've seen this in many other programs so i dont know what you are talking about.