我仍在努力学习 C++,我的第一个方法来自 C 的非对象方面。所以我想重做我的结构并利用类数据结构。我想出了这个。
在 Gradebook.h 中
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;
};
};
在我的 Gradebook.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_amount
给 Students
和 x_amount
的 Courses
。
问题
我不明白如何访问我的结构,所以我可以开始在我的结构中添加类(class)和学生。
我相信如果我可以尝试访问我的结构并操作其中的成员,我就可以完成剩下的工作。我试过:
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);
感谢对这个菜鸟的任何帮助。提前致谢!
最佳答案
您的整个设计可能需要一些工作。
您的结构应该自行初始化。他们在Gradebook
之外也是有意义的.
这是执行此操作的示例“C++ 方式”:
首先注意std::vector是一个动态数组。与其不必要地管理自己的内存,不如使用 std::vector
反而。使用 std::vector
, 你需要输入 #include <vector>
在文件顶部的某个位置。
对于 Student
, 我们删除了 count_student
多变的。一个容器应该跟踪学生的数量。 Student
现在还通过构造函数初始化自己的值。注意我是如何使用 constructor initialization list 的.
struct Student
{
Student () ;
int student_id;
std::string first;
std::string last;
};
Student::Student () : student_id (0)
{
}
我们为 Course
做类似的事情.再一次,这里重要的是 Course
正在初始化和管理自己的变量。
struct Course
{
Course () ;
int course_id;
std::string name;
std::vector <Student> students;
};
Course::Course () : course_id (0)
{
}
Gradebook
类现在被简化了。注意如何 NewCourse
需要 reference而不是指针。
class Gradebook {
public:
Gradebook () ;
void NewCourse (const Course &course) ;
private:
std::vector <Course> courses;
};
Gradebook::Gradebook ()
{
}
void Gradebook::NewCourse (const Course &course)
{
courses.push_back (course) ;
}
其他想法
我可能会取出 std::vector <Student> students;
来自 Course
.是的,一门类(class)可能有很多学生,一个学生也可能选修多门类(class)。这种多对多关系可能会让人头疼,也可能不会。
this link 中描述了一种处理方法.另一种方法是必须构建存储学生和类(class)对的结构。
更新
这是 std::vector::push_back 的函数签名:
void push_back (const value_type& val);
如您所见,该函数返回 void
, 所以你不能做类似 courses.push_back(id_number)->course_id
的事情.此函数仅将一个对象插入到您的 vector 中。
这是一个如何使用 std::vector
的例子与 Student
结构。首先,为了让事情变得更简单,我将向 Student
添加一个新的构造函数。 .请注意,我也更改了 student_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)
{
}
这是一个如何使用 vector 的例子。
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 ;
}
关于类中的c++动态结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161683/