0

我正在尝试使用我的双链表类建立一个学生列表。我最终将我的学生和 Node 类结合在一起来做到这一点。我意识到有更好的方法来实现这一点,但这是我选择的路径,我正在努力让它发挥作用。当我 push_back 一个节点(学生)时,它调用我的 Node 类,然后调用我的 Record 类,然后调用我的 Course 类。

在我的主函数中为一个节点调用我的构造函数时,我遇到了一个错误,即我传递的所有字符串都没有“(名称,课程,学期)未在此范围内声明”。我意识到这可能是一团糟,但是任何人都可以告诉我为什么这不起作用吗?

#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

我的节点/学生类:

#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
4

1 回答 1

0

我认为这条线list->push_back(John, Math34, Summer2012);是问题所在。

你没有传递字符串。将其替换为list->push_back("John", new Course("Math (course name)", "Math (faculty)", 34), "Summer2012");

于 2013-03-30T20:32:09.527 回答