ERROR: 'Student::NumCourses' : is not a type name

I get 2 errors and I don't know why I can't make int courses(NumCourses);
I get these errors:
'Student::NumCourses' : is not a type name
'NumCourses' : undeclared identifier
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
	int NumCourses;

	Student(string studentname, int studentcourses)
	{
		name = studentname;
		NumCourses = studentcourses;
	}


	string name;
	int courses[NumCourses];
	double cGPA, dGPA, pGPA;

};
int courses[NumCourses]; NumCourses is not a compile-time constant, so you cannot use it here.

change type of variable to int* and allocate memory in constructor: courses = new int[NumCourses];
Do not forget to delete memory in destructor.
When I do what you suggested above, I still get these errors:
1>c:\users\jas\documents\visual studio 2010\projects\project 3\project 3\Student.h(19): error C2327: 'Student::NumCourses' : is not a type name, static, or enumerator
1>c:\users\jas\documents\visual studio 2010\projects\project 3\project 3\Student.h(19): error C2065: 'NumCourses' : undeclared identifier
1>c:\users\jas\documents\visual studio 2010\projects\project 3\project 3\Student.h(19): error C2864: 'Student::courses' : only static const integral data members can be initialized within a class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
	int NumCourses;

	Student(string studentname, int studentcourses)
	{
		name = studentname;
		NumCourses = studentcourses;
	}


	string name;
	int* courses= new int[NumCourses];
	double cGPA, dGPA, pGPA;

};
Last edited on
Show your code
I put it in the post above!
why are you new'ing up an array like that?
Read MiiNiPaa's first post again.
int* courses= new int[NumCourses];int* courses;
At the end of constructor:
1
2
courses = new int[NumCourses];
}

Add the destructor to the class:
1
2
3
4
5
~Student()
{
    delete[] courses;
}
Ok, thanks it works now. :)
But would you mind explaining why I couldn't do that? I am fairly new to C++ and just want to know the details so I won't make the same mistake again.
The trick is that int* courses= new int[NumCourses]; is not something that should be done in the class itself. It should be defined in the class, but allocated in the constructor.

Try this:
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
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
	int NumCourses;

	Student(string studentname, int studentcourses)
	{
		name = studentname;
		NumCourses = studentcourses;
		courses = new int[NumCourses]; // Allocation done in the contructor
	}
	~Student()
	{
	    delete[] courses; // Remember to delete the memory in the destructor or you'll have a memory leak
	}

	string name;
	int* courses; // here it is defined
	double cGPA, dGPA, pGPA;
};
Last edited on
Thanks for the reply Stewbond :). I got it figured out with help of MiiniPaa just before you posted the solution. Thanks once again :).

But I am still wondering why I couldn't do it the other way before?
You're welcome, I was still writing while you guys were sorting it out.

The reason why you couldn't do it the other way before was that the size of courses[] is determined at compile time, not runtime. Therefore the compiler needs to know how much memory to save for this object. If you declare an array with a non-constant, then it means that you can change the size of the array at runtime. It is possible to do this with dynamic memory (which is what we introduced you to) because memory is taken from the "heap", but it is not possible to do this the conventional method because the memory is put into the "stack" whose size cannot be changed mid-function.
Ahh, makes so much sense now. Thank you very much, I really appreciate it. :)
Topic archived. No new replies allowed.