Error when I try to create array of object

I am getting quite a bit confused, to me I think the following two implementation are valid (but my syntax has error), but I can not get either one of them to work. This is a year 1 C++ intro class homework, we can not use Vector (or else it will be done a long time ago...)

I already finished my class Student, but now I have to create a container to store n students. I was thinking the following two option.

1) Create an array of student objects: Student arrayOfStudents[20];

My student class is very simple (It is modified different because too many lines). But the idea is like this...

class Student
{
    public:
    // Constructor for the Book class, take 6 arguments 
    Student(string myUID, string myLastName, string myFirstName, 
         string major, string age, string homeState, bool isWorking);
    ...
};

But when I try to create an array of students, I get this error message.

cs114p2main.cpp: In function ‘int main(int, char**)’:
cs114p2main.cpp:103: error: no matching function for call to ‘Student::Student()’
cs114p2Student.h:14: note: candidates are: Student::Student(std::string*)
cs114p2Student.h:11: note:                 Student::Student(std::string, std::string, std::string, std::string, std::string, bool)
cs114p2Student.h:7: note:                 Student::Student(const Student&)

2) Also tried: Create an array of student pointer that points to each student. Student* ArrayOfStudents = Student[20];

My question is why doesn't the method 1 work? And do I have to use pointers in this case (something along the line of method 2).

Because I need to pass this array into a function that going to set up each student. (I do not know how many students are going to be there). So does that mean I have to pass a pointer to the array of students? In that case I really hope method 1 can work, I can't imagine dealing with pointer to array of student pointers. I was thinking have to return an array of data[numOfData][numOfStudnet], and do it in the main, but either way I would like to figure out why this gives me error.. thank you.

Edit: Follwo up question : I know through research, I must declare the size of the array of Student as the parameter, but what if I only know the size after it is running? In that case, what are my options?

Answers


Your Student class has only one constructor, namely

Student(string myUID, string myLastName, string myFirstName,  
     string major, string age, string homeState, bool isWorking); 

Because of this, the default constructor does not get generated by the compiler. Since you also don't provide one (default ctor: one callable with no arguments), the compiler cannot create the array -- it would need default-constreuctable objects to place in the elements (initialize the array with).

To fix, you will need to provide a default ctor:

Student();

the behavior of which you will need to find out yourself based on how Student is assumed to behave.

You could also provide default arguments to all parameters of the listed Student ctor, so it is callable without explicitly specifying any of the parameters -- thus qualifies as a default ctor

If a default ctor is not feasable, you will need to become more creative and dyanmically allocate the array, copy all existing elements to the new one and assign the given new element to it when needed. Note that with this you are essentially re-creating the functionality of the std::vector container. Note also, that this approach is really hard to get it right, so I adwise against it, especially since you are just learning the basics.

Yet another approach is to create an array of pointers to Student, so you can assign each as you creaqte them dynamically. You will have to test whether the element is a null pointer, before you attempt to access the Student object pointed to.

Student* students[20];

students[0] = new Student(/*list of actual parameter values*/);

if (student[0] != NULL) {
  students[0]->getName(); // assuming you have a member function getName()
}

Note: the index 0 is used only for illustrative purposes, you can use a variable as well (as long as you ensure it styas within 0 and 19 (inclusive) -- note even though you have 20 elements, the largest index is 19 as the couning starts from 0


My question is why doesn't the method 1 work?

Because Student does not have a default constructor (a constructor with no arguments). A default constructor is generated by the compiler only if no constructors are defined. As constructors have been defined, which take at least one argument no default constructor exists. When you declare the array:

Student arrayOfStudents[20];

Twenty instances of Student are being created, using the default constructor which does not exist.

You could define a default constructor which would allow use of the array, but does it make sense for a Student to exist without a name, age, etc?

Or, use an array of Student*:

Student* arrayOfStudents[20] = {}; // The '{}' will set all pointers to NULL.

EDIT:

If the number of students is not known until runtime then:

// Get number of students somehow.
int numberOfStudents = ...;

// The '()' will set all pointers to NULL.
Student** arrayOfStudents = new Student*[numberOfStudents]();

Remember to delete each entry in arrayOfStudents and to delete[] the arrayOfStudents itself.


Need Your Help

How to reference WinRT components in Powershell

windows powershell windows-8 windows-runtime

I'm trying to use the Windows.System namespace, part of WinRT, in PowerShell.

How can I specify integer as a property or argument type in ColdFusion?

web-services soap types coldfusion wsdl

I'm building a WebService in ColdFusion 8 using CFCs with remote methods. I need to specify complex data types in the WSDL, so I'm creating CFCs for each of these types. For instance, let's say I h...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.