-2
class grades
{
private:
   char *grade;
   string course[20];
   int numCourse;
public:
   grades();
   bool setGrade(char *gradeIn);
   bool setCourse(char *nameIn);

};

So that's my class declaration. I want to set a constant number instead of 20 in the array declaration. How would I go about doing that? I have tried static const but the problem is that every time the class returns an error saying out of scope.

4

2 回答 2

3

This doesn't work?

class grades
{
private:
   char *grade;
   static const int MAX_COURSES = 20;
   string course[MAX_COURSES];
   int numCourse;
public:
   grades();
   bool setGrade(char *gradeIn);
   bool setCourse(char *nameIn);

};
于 2013-07-11T02:23:37.240 回答
1

Use a #define. Old school, but it still works.

or... declare a

 const int MAX_ARRAY_SIZE = 20

in a namespace (i.e. outside of the class).

于 2013-07-11T02:22:20.940 回答