c++;类内的动态结构 我仍然在学习C++,我的第一个方法来自C的非对象方面,所以我想重做我的结构并使用类数据结构。我想到了这个

c++;类内的动态结构 我仍然在学习C++,我的第一个方法来自C的非对象方面,所以我想重做我的结构并使用类数据结构。我想到了这个,c++,class,dynamic,struct,C++,Class,Dynamic,Struct,在成绩册上 class Gradebook { public: //default constructor, initializes variables in private Gradebook(); //accessor constructor, access members in private private: struct Student { int student_id; int count_student; string last; string fi

在成绩册上

class Gradebook {
public:
//default constructor, initializes variables in private
Gradebook();
//accessor constructor, access members in private
private:
struct Student {
    int student_id;
    int count_student;
    string last;
    string first;
    };
struct Course {
    int course_id;
    int count_course;
    string name;
    Student *students;
    };
};
在我的成绩册中.cpp

Gradebook::Gradebook() {

 Course *courses = new Course;
 courses->students = new Student;

 courses->count_course = 0;
 courses->course_id = 0;

 courses->students->count_student = 0;
 courses->students->student_id = 0;

}
我的设计

我想动态创建
x_金额
学生
x_金额
课程

问题

我不知道如何访问我的结构,以便开始在结构中添加课程和学生

我相信,如果我能尝试访问我的结构并操作其中的成员,我就可以完成剩下的工作。我试过:

void Gradebook::newCourse(Course *courses) {

  //do stuff

}
当我试图编译它时,会出现各种各样的错误:

In file included from gradebook.cpp:2:
./gradebook.h:9:18: error: C++ requires a type specifier for all declarations
        void newCourse(&Course);

gradebook.cpp:39:17: error: out-of-line definition of 'newCourse' does not match any
      declaration in 'Gradebook'
void Gradebook::newCourse(Course *courses) {


2 errors generated.
In file included from main.cpp:4:
./gradebook.h:9:18: error: C++ requires a type specifier for all declarations
        void newCourse(&Course);

非常感谢您对这个noob的任何帮助。提前谢谢

学生和课程被声明为私有结构,因此您无法直接访问它们

你有三个选择

  • 将单独的第一类对象作为它们自己的类
  • 使
    成绩册
    具有可创建和操作
    学生
    课程
    的界面
  • 在成绩册中公开学生和课程结构

  • 您的整个设计可能需要一些工作

    你的结构应该自己初始化。对于他们来说,置身于
    成绩册之外也是有意义的

    下面是一个“C++方法”示例:

    首先要注意的是一个动态数组。与其不必要地管理自己的内存,不如改用
    std::vector
    。要使用
    std::vector
    ,需要将
    #include
    放在文件顶部的某个位置

    对于
    Student
    ,我们删除了
    count\u Student
    变量。容器应记录学生人数
    Student
    现在还通过构造函数初始化自己的值。注意我是如何使用的

    我们为
    课程
    做类似的事情。同样,这里重要的是
    课程
    正在初始化和管理它自己的变量

    struct Course 
    {
        Course () ;
    
        int course_id;
        std::string name;
        std::vector <Student> students;
    };
    
    Course::Course () : course_id (0)
    {
    }
    
    其他想法

    我可能会拿出
    std::vector学生来自
    课程
    。是的,一门课程可能有许多学生,但一个学生也可能选修许多课程。这种多对多的关系可能会导致头痛,也可能不会

    中描述了处理此问题的一种方法。另一种方法是必须构建存储成对学生和课程的结构

    更新

    以下是函数签名:

    如您所见,该函数返回
    void
    ,因此您不能执行类似
    课程的操作。向后推(id\u编号)->课程id
    。此函数仅在向量中插入对象

    下面是一个如何将
    std::vector
    Student
    结构一起使用的示例。首先,为了让事情变得简单一点,我将向
    Student
    添加一个新的构造函数。请注意,我还将
    student\u id
    更改为
    id
    ,以尽量减少冗余

    struct Student 
    {
        Student () ;
        Student (const int id, const std::string &first, const std::string &last) ;
    
        int id;
        std::string first;
        std::string last;
    };
    
    Student::Student () : id (0)
    {
    }
    
    Student::Student (const int id, const std::string &first, const std::string &last)
        : id (id), first (first), last (last)
    {
    }
    
    下面是一个如何使用向量的示例

    int main (void)
    {
        // Create some students.
        Student s1 (1, "Bob", "Smith") ;
        Student s2 (2, "Katy", "Adams") ;
        Student s3 (3, "Mary", "Williams") ;
    
        // Create vector of students.
        std::vector <Student> students ;
    
        // Insert the students into the vector.
        students.push_back (s1) ;
        students.push_back (s2) ;
        students.push_back (s3) ;
    
        // Go through the students and print out data.
        for (unsigned n = 0; n < students.size (); ++n) {
            // Grab a reference to one of the students.
            Student &student = students [n] ; 
    
            // Print out information, be sure to #include <iostream> somewhere.
            std::cout << "ID: " << student.id
                << ", Name: " << student.first << " " << student.last << "\n" ;
        }
    
        // To update Bob's name to Rob.
        students [0].first = "Rob" ;
    
        return 0 ;
    }
    
    int main(无效)
    {
    //创造一些学生。
    学生s1(1,“鲍勃”、“史密斯”);
    学生s2(2,“凯蒂”、“亚当斯”);
    学生s3(3,“玛丽”、“威廉姆斯”);
    //创建学生向量。
    性病:病媒学生;
    //将学生插入向量。
    学生。推回(s1);
    学生。推回(s2);
    学生。推回(s3);
    //检查学生并打印数据。
    for(无符号n=0;nstd::cout为什么学生和Corse在成绩册课堂上?他们不是自己分开的对象吗?如果学生和课程在我的课堂外,我如何操纵他们?就像他们在课堂内一样。把他们放在“私人”教室里节,除了告诉编译器只有属于成绩册的代码才能访问它们之外,对对象没有任何作用。将它们放在成绩册类中不是很好的编码实践吗?@Soren您可能应该在回答中包括这样一个事实,即他在他的
    成绩册
    类中没有数据成员,并且他的构造函数都是错误的@jiv902,我明白了,我想知道如何在结构上使用vector。我尝试过使用vector cours;然后将它传递给一个函数,然后它说它是未知的。所以当我更新课程id时,我会使用cours。推回(id号)->课程id?我的意思是这样做的吗?我理解你给我的这个新概念,但我现在也有很多关于课程如何更新的问题。push_back更新课程id。你介意再解释一下吗?另外,我试过你的代码,我得到了以下错误。“错误:隐式声明默认构造函数的定义”关于课程::课程和学生:Student@user2816227您可能忘了在类定义中声明构造函数。另外,请确保您没有跳过任何分号或括号。有关详细信息,请参阅。我还更新了我的答案。希望它能解释更多关于您想要的内容。非常感谢您的支持代码的一部分。虽然我有更多的问题,我有答案。现在我有了一个开始学习的地方。我正在阅读你的链接和谷歌搜索各种各样的东西。我很感激。
    void push_back (const value_type& val);
    
    struct Student 
    {
        Student () ;
        Student (const int id, const std::string &first, const std::string &last) ;
    
        int id;
        std::string first;
        std::string last;
    };
    
    Student::Student () : id (0)
    {
    }
    
    Student::Student (const int id, const std::string &first, const std::string &last)
        : id (id), first (first), last (last)
    {
    }
    
    int main (void)
    {
        // Create some students.
        Student s1 (1, "Bob", "Smith") ;
        Student s2 (2, "Katy", "Adams") ;
        Student s3 (3, "Mary", "Williams") ;
    
        // Create vector of students.
        std::vector <Student> students ;
    
        // Insert the students into the vector.
        students.push_back (s1) ;
        students.push_back (s2) ;
        students.push_back (s3) ;
    
        // Go through the students and print out data.
        for (unsigned n = 0; n < students.size (); ++n) {
            // Grab a reference to one of the students.
            Student &student = students [n] ; 
    
            // Print out information, be sure to #include <iostream> somewhere.
            std::cout << "ID: " << student.id
                << ", Name: " << student.first << " " << student.last << "\n" ;
        }
    
        // To update Bob's name to Rob.
        students [0].first = "Rob" ;
    
        return 0 ;
    }