-1

I have struct

struct Course{
  int cId;
  string courseName;
};

and I want to add students for each Course. I thought to define another struct in to struct Course like

struct Course{
      int cId;
      string courseName;
      struct Student{
         int sId;
         string studentName;
      };
    };

if I define struct like this how could I use it ? I have Course * cptr = new Course[1]; which for using the struct course. How could I add Students to specified cId's ?

6
  • Nested structs? Why??? Is there any good reason to do that? Commented Nov 18, 2013 at 15:28
  • Add a member for the struct you have declared inside the other one. Commented Nov 18, 2013 at 15:28
  • 3
    You should have attended class when they taught arrays Commented Nov 18, 2013 at 15:28
  • @MichaelGoldshteyn: You should have attended class when they taught standard containers. Commented Nov 18, 2013 at 15:31
  • @phresnel, if you read the original poster's comments to answers, you will see that there was no such class! Commented Nov 18, 2013 at 15:33

5 Answers 5

5

Your Course does not contain a Student. It just defines a struct of that name, i.e. a type Course::Student. For a type to contain an instance of another type, you just have to declare a member variable:

struct Student { .... };

struct Course
{
  ....
  Student student;
};

If you want each course to hold more than one Student, then you can use a container. In the absence of more information, the best candidate for that is std::vector:

struct Course
{
  ....
  std::vector<Student> students;
};
Sign up to request clarification or add additional context in comments.

Comments

0
struct Course
{
    // ...
    struct Student
    {
        int sId;
        string studentName;
    };
};

This defines a struct Course and another struct Course::Student, but Course does not instantiate any student (there is no member of Course which has a type Student). If you want students to be a member of course, you need something like this:

struct Course
{
    // ...
    struct Student
    {
        int sId;
        string studentName;
    } student;
};

or

struct Course
{
    // ...
    struct Student
    {
        int sId;
        string studentName;
    } students[10];
};

Which would define a single student or an array of 10 students as members of Course, respectively. (NOTE: std::vector would be a better choice than a statically sized array)

Alternatively, you can declare Student as a struct outside of Course:

struct Student { ... };

struct Course
{
    // ...
    Student student;
    // or std::vector<Student> students;
    // or std::array<Student, 10> students;
    // or Student* students
};

4 Comments

but number of students could change the execution so, I should allocate memory dynamically. How could I do that ? instead of using student[10] ?
@mxyz You would use a pointer and allocated it using new[] (and deallocating using delete[]). If that is your requirement, the last // or in my last example would be the method you would need to use - if you are not permitted to use std::vector.
For example I have cptr[1].cId (it is unique) for specified course if I want to add students to this course how could I use stedents ?
Assuming that cptr[1] is an instance of Course, It would be something like cptr[1].students = new Student[SIZE]; cptr[1].students[0].sId = SOMEID;
0

A very simple way:

struct Student
{
    int sId;
    string studentName;
};

struct Course
{
    int cId;
    string courseName;
    std::vector <Student> students;
};

This way you may write

Student s;
Course c;
c.students.push_back(s);

Comments

0

std::vector is your friend, but if you are not allowed to use that, you can do:

struct Student {...}; // Implementation omitted for brevity...

struct Course
{
  Student *students_; // Use students_=new Student[num_students]; to allocate
                      // memory dynamically
};

Comments

0

I prefer you to divide this struct into two individual struct. Then you can initialize a student object in the Course struct. Or u can implement a function in Course struct to link with Student struct.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.