mardi 26 juin 2018

Why I get the following error: "returning reference to temporary [-Werror=return-local-addr]"

given the following code:

#include <set>

using std::set;

class Pool {
    set<int> s;
public:
    Pool();
    ~Pool();

    typedef typename set<int>::iterator Iterator;
    const Iterator& begin() const;
    // Iterator& begin();
    // const Iterator& end() const;
    // Iterator& end();
};

Pool::Pool() = default;
Pool::~Pool() = default;

const typename Pool::Iterator& Pool::begin() const {
    return s.begin(); //** error
}

Why I get the following error (I understand the meaning of the error, but, I don't understand why I get it in this case)?

returning reference to temporary [-Werror=return-local-addr]

How can I fix it?

Aucun commentaire:

Enregistrer un commentaire