Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#including Hello 'guys';

// i have a dump question, my mind isn't working now

HOW can i use these classes together(please_help_me A)?

------------------------------------------------------------------------------
C++
class student
{
   public:
      student();
      ~student();
      int age;
      int ID;
      // want MAX 10 year for a student
      Year YEAR[10];     //<------ ERROR
};
student::student()
{
   //What should i do for declaring Array YEAR
}

class Year
{
   public:
      Year();
      ~Year();
      string name;
      int _year;
      //want MAX 7 book for a year
      book BOOK[];      //<------ ERROR
      //book BOOK[7]; ???
};
Year::Year()
{
   //Same Problem . .. !
}

class book
{
   public:
      book();
      ~book();
      int ID;
      string name;
};
Posted

One solution would be linked list. other solution would be vector.

an example of using vector:
#include <vector>
class a
{};

std::vector<a> a_arr;

a ob;
a_arr.push_back(ob);
</a>
 
Share this answer
 
To keep it simple, in C++, a class must be defined before it is used in an array.

Thus in your case, you would have to define book class first, then Year class and finally student class.

As mentionned in another solution, using a vector might be a better idea... but it will not help for the fact that a class must be declared before it is used.

In some situations, a forward declaration can be used. That is only declare the type as in: class book;. But in your specific case, it would help since you need the full definition to declare an array of object.

For an array of pointers, it would be different but they are other problems like memory managment that is beyond the scope of this question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900