2012-04-15 10 views
1

私は学校のプロジェクトで作業しています。クラスのリンクリストを別のクラス(リンクされたリスト私はSTLクラスを使っています。今私はほぼを設定していますが、私の表示機能では、タスクの内容を表示するために、イテレータを使用しています。しかし、iteratorにtaskList.begin()を割り当てることはできません。エラーが出るからです。オペレータ "="はこれらのオペランドと一致します - 標準テンプレートライブラリの反復子

以下は、私が関係すると思うコードです。

objective.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

このメソッドはconstです。taskListはメンバーなので、非反復子を使用することはできません。const

メンバメソッドを作成するconstは、そのメソッドが非mutableクラスメンバを変更したり、非 - constメンバメソッドを呼び出さないという契約です。非constイテレータを持つことによって、あなたはその契約を破棄しています。

displayがconstのであるので、あなたは、constのイテレータを使用することができます

std::list<task>::const_iterator taskIterator; 
関連する問題