Array in a Constructor Help

Hello,
I am looking help on building a constructor with an array as an argument.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;
class student
{
	
	public:
		student();
		student(string StudentName);
		student(int ClassCount);
		student(string classNames[]);
		student(string StudentName, int ClassCount );
		student(string StudentName, int ClassCount, string classNames[]);												
	private:
		string name = "";			//name of Student
		int numClasses = 0;
		string classList[];
				
};
int main()
{
	student carlos("carlos");
	return 0;
}

student::student()
			{}
student::student(string StudentName)
			: name(StudentName)
			{}
student::student(string StudentName, int ClassCount, string classNames[])
			:name(StudentName), numClasses(ClassCount),    classList(classNames)
			{}			



the error points to line 34:
[Error] invalid initializer for array member 'std::string student::classList [0]'

I am just confused i guess because I cant really find a source that mentions how to do this. thanks.
Really there are two problems with this code.
main.cpp|19|error: ISO C++ forbids zero-size array ‘classList’ [-Wpedantic]|
main.cpp||In constructor ‘student::student(std::string, int, std::string*)’:|
main.cpp|34|error: invalid initializer for array member ‘std::string student::classList [0]’|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|

For the second error you're probably going to need to initialize this array inside the constructor body.

1
2
3
4
5
6
student::student(string StudentName, int ClassCount, string classNames[])
			:name(StudentName), numClasses(ClassCount), classList() 
			{
                           for(int i = 0; i < numClasses; ++i)
                              classList[i] = classNames[i];
			}


Last edited on
Thank you for the reply jlb,

how can i create an object .. the part i am not getting is with the array again
i tried the following below

 
student joey("joey", 5, {"MAT211", "CHM101", "ENG101"});


this isnt working obviously. I guess i am not sure how to ask the question. thank you

regards
Last edited on
http://www.informit.com/articles/article.aspx?p=1852519


However, plain arrays? How about std::vector<std::string>>?
closed account (48T7M4Gy)
It depends whether you want to create the array dynamically or not.

If the array is always the same size then just create it as you would normally with appropriate gets and sets using ponters.

If the size of the array isn't known at compile time then you have to create it using new, delete etc passing the required size to the class member function at runtime. You still use pointers along with gets and sets as appropriate. In the constructor there are several ways of passing the subject list to the class.

http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/doc/tutorial/dynamic/

Topic archived. No new replies allowed.