Thread: Creating Objects + Array

  1. #1
    Registered User Cpro's Avatar
    Join Date
    Oct 2006

    Creating Objects + Array

    I have a class called "Student". It has members such as first name, last name, gpa...etc. In this program, i need to create and array, and be able to add objects into the array. Now, i know how to create an object before running the program:
    Student Stud1("Williams","John", 4.0);
    however, i am not sure how to create an object whenever the user is using the program. Using a struct would be something like:
    new Student;
    cin Student.First_Name;
    but i'm not sure how to do it with objects.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You might consider using a std::vector<Student> for an 'array' of Student objects.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    I'm not familiar with that. I tried creating an array:
    Student Array1[10];
    but it came up with an error: 'Student' : no appropriate default constuctor available.
    "Student" is a derived class of the base class "Individual".
    Here is my code for the constructors:

    I'm not sure why it's not letting me create the array.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Because those classes don't have default constructors. That means that the compiler cannot create 10 objects for the array because it needs strings to call the constructor for each object.

    You could create a default constructor and an initialization function, then create the array and set those variables with the init function. You could also use a vector, which lets you use the constructors you have. However, that won't help much either because you would still have to give all 10 Students the same initial data.

    So I would create a default constructor (maybe by just giving default empty string values for your strings. Then set the data later.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Spreadsheet using a multidimensional array
    By Apiatan in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2009, 04:18 PM
  2. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Help with Array for objects
    By curwa in forum C++ Programming
    Replies: 5
    Last Post: 05-17-2003, 11:21 AM
  5. dynamic array of objects
    By mrukok in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2003, 09:35 AM