Dynamic object

I am creating a student object and want to call my constructor using a loop here is my code can anyone tell me what i am doing wrong.
I want the loop to run and each time access the constructor and ask for input



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int N = 0;
    cout<<"Enter the number of students to be created"<<endl;
    cin>>N;

// the ** makes the array of pointer not array of objects
Student **pointerArray;

// crerate all the Pointer
pointerArray = new (Student*)[N];

// create all the objects
for (int i=0; i< N; i++)
{
pointerArray[i] = new Student(i);
}
Last edited on
As for the student info in the loop. I don't think you need new Student(i), you need new Student(info), where info is the student info you asked the user to enter in the loop.
Not sure what you mean, the (i) in the loop is the counter.
my compiler keeps highlighting - Student **pointerArray;
Not sure whats wrong with it?
What error does it return?

-Albatross
\main.cpp conflicting declaration 'Student**studentObject'
Last edited on
1
2
3
4
5
6
7
pointerArray = new (Student*)[N];

// create all the objects
for (int i=0; i< N; i++)
{
pointerArray[i] = new Student(i);
}


That looks to me like a double initialization. Why are you reinitializing Student?

-Albatross
I have fixed that, the current error is
\main.cpp array bound forbidden after parenthesized type-id





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main(int argc, char *argv[])
{
    int N = 0;
    cout<<"Enter the number of students to be created"<<endl;
    cin>>N;
// the ** makes the array of pointers not an array of objects
Student **studentObject;
// create all the Pointers
studentObject = new (Student*)[N];

// create all the objects
for (int i=0; i< N; i++)
{
studentObject[i] = new Student(i);   
}
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
Last edited on
"That looks to me like a double initialization. Why are you reinitializing Student?"

Not sure what you mean?
Topic archived. No new replies allowed.