成员函数中的迭代器问题

时间:2018-09-23 20:43:10

标签: c++ vector stl iterator

#ifndef User_hpp
#define User_hpp
#include <stdio.h>
#include "Account.hpp"
#include "Associazione.hpp"
#include <vector>

class User : public Account{

private:
    string date;
    string surname;
    string address;
    vector<Associazione> links;

public:
    void iterateVector() const;
...

现在,如果我要迭代“链接”矢量,则如下所示:我已经将方法“ iterateVector()”开发为.cpp文件,如下所示:

#include "User.hpp"
#include <iostream>

User::User(){

}

void User::iterateVector() const{
for (vector<Associazione>::iterator it = links.begin(); it != 
     links.end(); ++it){
}

但是编译器向我返回错误:

  

从'__wrap_iter std :: __ 1 :: vector Associazione,std :: __ 1 :: allocator Associazione :: const_pointer'到'__wrap_iter'没有可行的转换

有人可以帮我吗?..

1 个答案:

答案 0 :(得分:0)

void User::iterateVector() const{
for (vector<Associazione>::iterator it = links.begin(); it != 
     links.end(); ++it){
}

应该是

void User::iterateVector() const{
for (vector<Associazione>::const_iterator it = links.begin(); it != 
     links.end(); ++it){
}

因为User :: iterateVector()是const成员函数,承诺不会更改任何成员。