the code send error in this line course["CS"].student=new Course*[1];
代码在此行课程中发送错误[“CS”]。student = new Course * [1];
i want create linked list of courses contain linked list of students
我想创建链接的课程列表包含学生的链接列表
here is the code
这是代码
struct Student{
string name;
int id;
int grade;
Student(string n, int i, int gd ){
name=n;
id=i;
grade=gd;
}
};
struct Course{
string C_Name;
Student **student;
int index;
void add_student(Student *new_student){
student[++index]=new_student;
}
};
Course course[4];
void init(){
course["CS"].student=new Course*[1];
}
2 个解决方案
#1
Your code doesn't contain any kind of linked list, but only plain arrays. Apart from that, the last line (course["CS"].student=new Course*[1];
) contains some invalid syntax.
您的代码不包含任何类型的链接列表,但只包含普通数组。除此之外,最后一行(course [“CS”]。student = new Course * [1];)包含一些无效的语法。
- An integral or enum type must be used to access an array (
string
s orchar[]
wont work) - Assigning a
Course**
to aStudent**
object is not allowed
必须使用整数或枚举类型来访问数组(字符串或char []不起作用)
不允许将课程**分配给学生**对象
A linked list contains of nodes each having a pointer to the next node. The last node usually has a pointer with value nullptr
(C++11) or 0
(older standard). Note: There is also a so-called double linked list, where every node also stores a pointer to the previous one. A node contains all the data, that you want it to store. Example:
链表包含节点,每个节点具有指向下一节点的指针。最后一个节点通常有一个值为nullptr(C ++ 11)或0(旧标准)的指针。注意:还有一个所谓的双链表,其中每个节点还存储指向前一个节点的指针。节点包含您希望它存储的所有数据。例:
struct Node {
Node* next;
// other node data here
};
To create a linked list, you first start with one node and set next = nullptr; // 0
. To add another node just create a new one and change the pointer of the first one. Example:
要创建链接列表,首先要从一个节点开始,然后设置next = nullptr; // 0.要添加另一个节点,只需创建一个新节点并更改第一个节点的指针。例:
Node* node1 = new Node();
node1 -> next = nullptr;
Node* node2 = new Node();
node2 -> next = nullptr;
node1 -> next = node2;
You start to see a pattern. To insert at the front just create a new Node
and set its next
to the first already existing node. To insert between two nodes, say node1
and node2
:
你开始看到一种模式。要在前面插入,只需创建一个新节点并将其设置为第一个已存在的节点。要在两个节点之间插入,例如node1和node2:
node1 -> next = newNode;
newNode -> next = node2;
To make it nice one usually writes a wrapper class containing functions such as
为了使它更好,人们通常会编写一个包含类似函数的包装类
InsertNodeAt(Node* node, uint index);
Node* GetNodeAt(uint index);
RemoveNodeAt(uint index);
Since you have two different types of objects (Student
and Curse
), you might want to use templates and avoid writing a linked list class for each type.
由于您有两种不同类型的对象(学生和诅咒),您可能希望使用模板并避免为每种类型编写链接列表类。
If you want to create your linked list yourself, I recommend doing some additional research (google is your friend) as I have only mentioned a few things.
如果您想自己创建链接列表,我建议您做一些额外的研究(谷歌是您的朋友),因为我只提到了一些事情。
If you don't mind using the c++ standard library you might be interested in using the already pre-made linked list classes std::forward_list
(standard linked list) and std::list
(double linked list).
如果您不介意使用c ++标准库,您可能会对使用已经预先制作的链表类std :: forward_list(标准链表)和std :: list(双链表)感兴趣。
#2
in c++ you didn't defined course["string"], so you may not use "CS" as an index to a Course type object and *.student is a class Student's object, not Course type
在c ++中你没有定义course [“string”],所以你不能使用“CS”作为Course类型对象的索引而* .student是类的Student对象,而不是Course类型
#include <iostream>
#include <stdexcept>
using namespace std;
struct Student{
string name;
int id;
int grade;
Student(string n, int i, int gd ){
name=n;
id=i;
grade=gd;
}
};
struct Course{
string C_Name;
Student **student;
int index;
void add_student(Student *new_student){
student[++index]=new_student;
}
};
Course course[4];
void init(){
// Need allocate space for a new object of class "Course"
Course course;
course.student = new Student*[1];// "student" is Student type but not Course
}
int main()
{
try{
init();
}
catch(...){
return -1;
}
std::cerr <<"your debug info" <<endl;
return 0;
}
And in my own opinion, in c++ you may try reference and it's counter in class Course's definition to class Student. Using printer in this way may cause error unexpected.
而在我看来,在c ++中你可以尝试参考,它可以在班级课程的定义中反对班级学生。以这种方式使用打印机可能会导致意外错误。
#1
Your code doesn't contain any kind of linked list, but only plain arrays. Apart from that, the last line (course["CS"].student=new Course*[1];
) contains some invalid syntax.
您的代码不包含任何类型的链接列表,但只包含普通数组。除此之外,最后一行(course [“CS”]。student = new Course * [1];)包含一些无效的语法。
- An integral or enum type must be used to access an array (
string
s orchar[]
wont work) - Assigning a
Course**
to aStudent**
object is not allowed
必须使用整数或枚举类型来访问数组(字符串或char []不起作用)
不允许将课程**分配给学生**对象
A linked list contains of nodes each having a pointer to the next node. The last node usually has a pointer with value nullptr
(C++11) or 0
(older standard). Note: There is also a so-called double linked list, where every node also stores a pointer to the previous one. A node contains all the data, that you want it to store. Example:
链表包含节点,每个节点具有指向下一节点的指针。最后一个节点通常有一个值为nullptr(C ++ 11)或0(旧标准)的指针。注意:还有一个所谓的双链表,其中每个节点还存储指向前一个节点的指针。节点包含您希望它存储的所有数据。例:
struct Node {
Node* next;
// other node data here
};
To create a linked list, you first start with one node and set next = nullptr; // 0
. To add another node just create a new one and change the pointer of the first one. Example:
要创建链接列表,首先要从一个节点开始,然后设置next = nullptr; // 0.要添加另一个节点,只需创建一个新节点并更改第一个节点的指针。例:
Node* node1 = new Node();
node1 -> next = nullptr;
Node* node2 = new Node();
node2 -> next = nullptr;
node1 -> next = node2;
You start to see a pattern. To insert at the front just create a new Node
and set its next
to the first already existing node. To insert between two nodes, say node1
and node2
:
你开始看到一种模式。要在前面插入,只需创建一个新节点并将其设置为第一个已存在的节点。要在两个节点之间插入,例如node1和node2:
node1 -> next = newNode;
newNode -> next = node2;
To make it nice one usually writes a wrapper class containing functions such as
为了使它更好,人们通常会编写一个包含类似函数的包装类
InsertNodeAt(Node* node, uint index);
Node* GetNodeAt(uint index);
RemoveNodeAt(uint index);
Since you have two different types of objects (Student
and Curse
), you might want to use templates and avoid writing a linked list class for each type.
由于您有两种不同类型的对象(学生和诅咒),您可能希望使用模板并避免为每种类型编写链接列表类。
If you want to create your linked list yourself, I recommend doing some additional research (google is your friend) as I have only mentioned a few things.
如果您想自己创建链接列表,我建议您做一些额外的研究(谷歌是您的朋友),因为我只提到了一些事情。
If you don't mind using the c++ standard library you might be interested in using the already pre-made linked list classes std::forward_list
(standard linked list) and std::list
(double linked list).
如果您不介意使用c ++标准库,您可能会对使用已经预先制作的链表类std :: forward_list(标准链表)和std :: list(双链表)感兴趣。
#2
in c++ you didn't defined course["string"], so you may not use "CS" as an index to a Course type object and *.student is a class Student's object, not Course type
在c ++中你没有定义course [“string”],所以你不能使用“CS”作为Course类型对象的索引而* .student是类的Student对象,而不是Course类型
#include <iostream>
#include <stdexcept>
using namespace std;
struct Student{
string name;
int id;
int grade;
Student(string n, int i, int gd ){
name=n;
id=i;
grade=gd;
}
};
struct Course{
string C_Name;
Student **student;
int index;
void add_student(Student *new_student){
student[++index]=new_student;
}
};
Course course[4];
void init(){
// Need allocate space for a new object of class "Course"
Course course;
course.student = new Student*[1];// "student" is Student type but not Course
}
int main()
{
try{
init();
}
catch(...){
return -1;
}
std::cerr <<"your debug info" <<endl;
return 0;
}
And in my own opinion, in c++ you may try reference and it's counter in class Course's definition to class Student. Using printer in this way may cause error unexpected.
而在我看来,在c ++中你可以尝试参考,它可以在班级课程的定义中反对班级学生。以这种方式使用打印机可能会导致意外错误。