Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
#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)?
------------------------------------------------------------------------------
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 24-Dec-12 23:17pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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>
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 595
1 OriginalGriff 295
2 CPallini 210
3 PIEBALDconsult 150
4 Fredrik Bornander 140
0 OriginalGriff 5,835
1 Sergey Alexandrovich Kryukov 5,263
2 CPallini 4,750
3 George Jonsson 3,227
4 Gihan Liyanage 2,487


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 25 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100