Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12

    Dynamic Object Name


    Hi all, looking for a little help with dynamic object names.

    Eg, i have a class for students. When the system is run, the user will be asked how many students they require, the program can then create X instances of the student class,

    student1, student2... studentX etc.

    Im just wondering how todo this. Is Student student[i](); correct for a constructor? If not, how do I do it ?

    Cheers
    Taz
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    the constructor should remain "standard":
    Student();

    to create the objects:
    Code:
    Student student[];
    ...
    for (i=0;i<students_number;i++) {
      student[i]=new Student();
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12
    thank you very much young sir, will report back when i balls it up :p

    Cheers
    Taz
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12
    one thing, I need to pass 2 arguments to my constructor.

    So does...
    PHP Code:
    Student student[];
    ...
    for (
    i=0;i<students_number;i++) {
      
    student[i]=new Student();

    become
    PHP Code:
    Student student[];
    ...
    for (
    i=0;i<students_number;i++) {
      
    student[i]=new Student([b]MY ARGUMENTS?[/b]);

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12
    right, on compilng the following code I get the following errors..

    int num_students = 2;
    Students students[];

    // Generate required number of monkey objects
    int i;
    for(i = 0; i < num_students; i++)
    {
    students[i]=new Students(5,3);
    }
    Errors:
    stage3.cc: In function `int main()':
    stage3.cc:24: array size missing in `students
    stage3.cc:24: no matching function for call to `Students::Students()'
    monkey.h:14: candidates are: Students::Students(const Students&)
    students.h:25: Students::Students(int, int)
    stage3.cc:30: no match for `Students& = Students*&' operator
    students.h:14: candidates are: Students& Students::operator=(const Students&)
    Side note. The class works when calling normally, eg Students students(5,3);
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Try declaring your array like this:
    Code:
    int num_students = 2;
    Student *students = new Student[num_students](5,3);
    ... 
    ... do your stuff
    ...
    delete [] students;
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    From your error message it looks like you don't have a constructor defined that takes two arguments, but that is the way you would call the constructor. A constructor is like any other function except it doesn't and can't have a return type, and when you call any function, the parameters must match up with a function definition.

    student[i]=new Student(MY ARGUMENTS?);
    ---------------

    Student student[];******what size array did you create here?

    for (i=0;i<students_number;i++) {
    student[i]=new Student();
    }
    Last edited by 7stud; March 9th, 2003 at 02:06 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    236
    Rep Power
    12
    Hi guys, the following fixed it, required pointers. Note, student is now monkey :)

    Regards

    // Create pointer array
    int num_monkeys = 2;
    Monkey *monkey[num_monkeys];

    // Generate required number of monkey objects
    for(int i = 0; i < num_monkeys; i++)
    {
    monkey[i] = new Monkey(5,3);
    }
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    13
    7stud was right. some clarifications.
    What you are trying to do, as you probably know, is to dynamically allocate an array of

    Student.
    You have to use pointers since you don't know the size of the array.

    before that you have some problems with the constructor.
    let's take the following class:
    PHP Code:
    class Student
    {
    public:
        
    Student(int iint j)
        {
            
    nClassID i;
            
    nStudentID j;
        }
    private:
        
    int nClassID;
        
    int nStudentID;

    let's say you want to retrieve the number of the students, as well as the IDs, from the user

    and than construct these instances.
    This is the wrong way:
    PHP Code:
    int nNumberOfStudents;
    Student *pStudentsArray;
    // get the number of students from the user and put it in nNumberOfStudents
    // than dynamically allocate the array
    pStudentsArray = new Student[nNumberOfStudents]; 
    why? because when you try to allocare the "new Student[..]" the program tries to construct

    these instances.

    you have to possibilities as far as I know:
    1. Use pointer to pointer (I will explain)
    2. Add a function "Create(int i, int j)" and empty the constructor.

    Explaination:
    1. Instead of allocating an array of students we allocate an array of pointers to Student
    PHP Code:
    typedef Student *PStudent;
    // get number of students from the user
    PStudent *ppStudentsArray = new PStudent[nNumberOfStudents];
    for (
    int i=0i<nNumberOfStudentsi++)
    {
        
    int nClsIDnStudentID;
        
    //get values from the user to nClsID and nStudentID
        
    ppStudentsArray[i] = new Student(nClsIDnStudentID);

    2. Add Create(int, int) that does the same thing as the constructor. make the constructor do

    NOTHING and do the following:
    PHP Code:
    // get number of students from the user
    Student *pStudentsArray = new Student[nNumberOfStudents];
    for (
    int i=0i<nNumberOfStudentsi++)
    {
        
    int nClsIDnStudentID;
        
    //get values from the user to nClsID and nStudentID
        
    pStudentsArray[i].Create(nClsIDnStudentID);

    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    13
    Your solution is good only if the number of monkeys is constant - but if you want to get this and the other values from the user it wont work i suspect.
    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    >> 2. Add Create(int, int) that does the same thing as the constructor. make the constructor do nothing
    Or, you could use my example posted above and use the constructor just fine. BTW I tested my example with g++ before posting it here :).
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    13
    It would work - but i'm not sure it will satisfy all the requirements:
    1. The number of Elements is CONSTANT and not dynamic
    2. What if you want to construct the each element with different values?!
    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.
  24. #13
  25. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    >> 1. The number of Elements is CONSTANT and not dynamic
    Nope, you can get num_students from user input.

    >> 2. What if you want to construct the each element with different values?!
    Then you'd have to have a constructor with no arguments and set the values inside a for loop, like your example did :).
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    ISRAEL
    Posts
    35
    Rep Power
    13
    Originally posted by Scorpions4ever
    >> 1. The number of Elements is CONSTANT and not dynamic
    Nope, you can get num_students from user input.

    >> 2. What if you want to construct the each element with different values?!
    Then you'd have to have a constructor with no arguments and set the values inside a for loop, like your example did :).
    I have checked it on VC++ and you cannot get num_students from the user.
    if you have succeeded please post the code - prove me wrong.
    (the previous code you posted doesn't work)
    "Gravitation can NOT be responsible for people falling in Love"
    (one of the most significant characters in the history, can you guess?)

    Gmorph.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Scorpions4ever is right, as he/she well knows, and has always been the case in my experience. :) It's pretty trivial to prove, but here's an example anyway (using int as the type rather than Student) compiled and run in VC++6:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int students = 0;
    	cout<<"Enter the number of students:\n";
    	cin>>students;
    
    	int* scores = new int[students];
    
    	for(int i=0; i<students; i++)
    		scores[i] = i;
    	for(int ii=0; ii<students; ii++)
    		cout<<scores[ii]<<endl;
    
    	delete [] scores;
    	return 0;
    }
    Normally, an array size must be determinable at compile time, so the memory can be set aside, however when you're allocating memory dynamically at run time, the size of an array doesn't need to be known until run time.
    Last edited by 7stud; March 9th, 2003 at 05:01 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo