没有运算符"="与这些操作数匹配 - 标准模板库中的迭代器
No operator "=" matches these operands - Iterators from the Standard Template Library
好的,我正在为学校做一个项目,我们需要在另一个班级中有一个类的链接列表(一个名为"目标"的类内类"任务"的链表),所以为此我使用 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
目标.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();...)有人知道问题的原因吗?如有必要,我还可以提供更多代码。提前谢谢你!
该方法是 const
,taskList
是成员,因此您不能在其上使用非const
迭代器。
使成员方法const
是一种协定,该方法不会更改非mutable
类成员,也不会调用非const
成员方法。通过拥有一个非const
迭代器,你正在破坏这个合同。
由于display
是常量,因此您可以使用 const 迭代器:
std::list<task>::const_iterator taskIterator;
相关文章:
- 如何使用 std::list 模板的迭代器擦除
- 对于C字符串,是否有一个标准的C++迭代器
- 如何避免为模板化迭代器的每个可能的实例化专门化iterator_traits?
- 如何为自定义模板化迭代器实现 std::d istance()
- 推进标准映射的迭代器
- boost :: iterator_range从模板的迭代器中
- 传递给<N>回调函数的模板化迭代器
- 模板容器迭代器
- 如何检查是否可以推进简单C++标准库迭代器
- 为什么插入会使标准::设置反向迭代器无效
- 没有运算符"="与这些操作数匹配 - 标准模板库中的迭代器
- 如何使用(模板)迭代器值创建集合
- c++模板和迭代器
- 模板比较迭代器
- C++11:模板化迭代器描述的基于范围的 for 循环
- 永不结束的STD库迭代器
- 将基于自定义模板的迭代器类对象转换为const_iterator
- 当泛化STL容器不知道迭代器的值类型时,如何使用模板化迭代器
- 通过不使用模板的迭代器返回std::list
- 为什么标准随机访问迭代器不实现size_t算术?