1

好的,我正在为学校做一个项目,我们需要在另一个班级中有一个班级的链表(一个名为“目标”的班级中的班级“任务”的链表),所以为此我' m 使用 STL 类。现在我几乎已经设置好了,但是在我的显示功能中,为了显示任务的内容,我使用了一个迭代器。但是我不能将 taskList.begin() 分配给迭代器,因为它给了我一个错误。

以下是我认为相关的代码。

目标.h

    #ifndef OBJECTIVE_H
    #define OBJECTIVE_H
    #include <string>
    #include <list>
    #include "date.h"
    #include "task.h"
    using namespace std;
    namespace team2
    {
      class objective
      {
            private:
                string objective_name, objective_desc, resources[10];
                int category, priority, res_used;
                double time;
                date start, end;
                int status;
                std::list<task> taskList;

            public:
                // CONSTRUCTORS
                objective();
                objective(string objN, string objD, int c, int p, date s, date e, double t, string res[], int resU, int stat, list<task>& tList);

...

                // CONSTANT MEMBER FUNCTIONS
                void display() const;
...
      };
    }
    #endif

Objective.cpp(这是我得到错误的地方)

#include "objective.h"
#include "date.h"
#include <cstdlib>
#include <cassert>
#include <string>
#include <list>
#include "task.h"
using namespace std;

namespace team2
{
    void objective::display() const // display() - Displays the complete contents of a single objective
    {
        int days, hours, minutes;
        std::list<task>::iterator taskIterator;

        days = floor(time/24.0); // Find the max number of days based off of the time (in hours)
        hours = floor(time - days*24); // Find the max number of hours after deduction of days
        minutes = floor((time - (days*24 + hours))*60); // Find the number or minutes after taking into account hours and days

        cout << "\nObjective Name: " << objective_name << endl;
        cout << "Objective Description: " << objective_desc << endl;
        cout << "Category: Quad " << category << endl;
        cout << "Priority: " << priority << endl;
        cout << "Starting Date: " << start.getMonth() << "/" << start.getDay() << "/" << start.getYear() << endl;
        cout << "Ending Date: " << end.getMonth() << "/" << end.getDay() << "/" << end.getYear() << endl;
        cout << "Time Required: " << days << " Days " << hours << " Hours " << minutes << " Minutes " << endl;
        cout << "Resources: " << endl;
            if(res_used == 0)
                cout << "\tNo Resources" << endl;
            for(int i = 0; i < res_used; i++)
                cout << "\t" << resources [i] << endl;

        cout << "Current Status: ";
            if(status == 1)
                cout << "Completed" << endl;
            else if(status == 0)
                cout << "Incomplete" << endl;
        cout << "Tasks: " << endl;
            if(taskList.empty())
                cout << "\tNo Resources" << endl;
            for(taskIterator = taskList.begin(); taskIterator != taskList.end(); taskIterator++)
            {
                 (*taskIterator).display();
                 cout << endl;
             }
    }
}

任务类与目标类几乎相同,省略了一些字段。错误发生在 for 循环中。for(taskIterator = taskList.begin();...) 有人知道问题的原因吗?如有必要,我还可以提供更多代码。先感谢您!

4

1 回答 1

4

该方法是const,taskList是一个成员,因此您不能在其上使用非const迭代器。

制作成员方法const是一种约定,该方法不会更改非mutable类成员,也不会调用非const成员方法。通过使用非const迭代器,您将违反该合同。

由于display是 const,您可以使用 const 迭代器:

std::list<task>::const_iterator taskIterator;
于 2012-04-15T23:05:38.743 回答