我正在尝试使用我的双链表类构建学生列表。我最终将我的学生和Node类组合在一起来完成这个任务。我意识到有更好的方法来实现这一点,但这是我选择的道路,我正在努力让它发挥作用。当我push_back一个节点(Student)时,它调用node类,node类再调用Record类,Record类再调用Course类。
在main函数中调用节点的构造函数时,我遇到了错误,我传递的字符串"(Name, course, semester)没有在此作用域中声明"。我知道这可能是一个烂摊子,但谁能给我一个主意,为什么这不是工作?
#include <iostream>
#include "LinkedList.h"
#include "Stack.h"
#include "Queue.h"
using namespace std;
int main () {
LinkedList* list = new LinkedList();
list->push_back(John, Math34, Summer2012);
return 0;
}
我的LinkedList类。我去掉了大部分的函数,这样更容易阅读和找到问题的根源。
#ifndef LinkedList_H
#define LinkedList_H
#include "Node.h"
#include <iostream>
using namespace std;
class LinkedList{
public:
LinkedList()
{
front = NULL;
back = NULL;
size = 0;
}
void aCourse(string n, Course* c, string s)
{
if (front == NULL)
return;
Node *temp = front;
for(int i = 0; i < size; i++)
{
if(n == temp->name)
front->addCourse(c, s);
} }
void push_back(string n, Course* c, string s)
{
if (back == NULL)
{
back = new Node(NULL, NULL, n);
front = back;
size++;
back->addCourse(c, s);
return;
}
else {
Node *newnode = new Node(NULL, NULL, n);
newnode->addCourse(c, s);
back->next = newnode;
newnode->prev = back;
back = newnode;
}
#endif
My Node/Student class:
#ifndef NODE_H
#define NODE_H
#include "Record.h"
#include "Course.h"
class Node
{
public:
Node(Node* n = NULL, Node* p = NULL, string v = NULL)
{
prev = p;
next = n;
name = v;
rec = new Record;
}
void addCourse(Course* c, string sem)
{
rec->addCourse(c, sem);
}
void dropCourse(Course* c)
{
rec->dropCourse(c);
}
Record* getRecord() { return rec; }
void printAllRecords()
{
rec->print();
}
void setStudentScore(Course* c, int score)
{
rec->setCourseScore(c, score);
}
string name;
Record* rec;
Node* next;
Node* prev; //for double linked list
char value;
};
#endif
我的记录类:
#ifndef Record_H
#define Record_H
#include "Course.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Record
{
public:
Record()
{
courses = new vector<Course*>();
semesters = new vector<string>();
scores = new vector<int>();
}
~Record()
{
delete courses;
delete semesters;
delete scores;
}
void addCourse(Course* c, string sem)
{
courses->push_back(c);
semesters->push_back(sem);
scores->push_back(0);
}
void dropCourse(Course* c)
{
vector<Course*>::iterator it = courses->begin();
vector<string>::iterator it2 = semesters->begin();
while ( it != courses->end() && it2 != semesters->end())
{
if (c == *it)
break;
it++;
it2++;
}
courses->erase(it);
semesters->erase(it2);
}
void setCourseScore(Course* c, int g)
{
vector<Course*>::iterator it = courses->begin();
vector<int>::iterator it2 = scores->begin();
while ( it != courses->end() && it2 != scores->end())
{
if (c == *it)
break;
it++;
it2++;
}
it2 = scores->insert(it2, g);
}
void computeAccGPA()
{
}
vector<Course*>* getCourses() { return courses; }
void print(){
vector<Course*>::iterator it = courses->begin();
vector<string>::iterator it2 = semesters->begin();
vector<int>::iterator it3 = scores->begin();
while ( it != courses->end() && it2 != semesters->end() && it3 != scores->end())
{
(*it)->print();
cout<<" "<<*it2<<" "<<*it3<<endl;
it++;
it2++;
it3++;
}
cout<<endl;
}
private:
vector<Course*>* courses;
vector<string>* semesters;
vector<int>* scores;
};
#endif
我的课程:
#ifndef Course_H
#define Course_H
#include <string>
#include <iostream>
using namespace std;
class Course
{
public:
Course(string n, string f, int c)
{
name = n;
faculty = f;
credit =c;
}
~Course() {}
void print()
{
cout<<name<<" "<<faculty<<" ";//<<c<<" ";
}
private:
string name;
string faculty;
int credit;
};
#endif
我认为list->push_back(John, Math34, Summer2012);
这一行有问题。
你没有传递字符串。替换为list->push_back("John", new Course("Math (course name)", "Math (faculty)", 34), "Summer2012");