C++11
使用基于范围的 for 循环迭代作为类成员的 std::vector 的代码是什么?我已经尝试了以下几个版本:
struct Thingy {
typedef std::vector<int> V;
V::iterator begin() {
return ids.begin();
}
V::iterator end() {
return ids.end();
}
private:
V ids;
};
// This give error in VS2013
auto t = new Thingy; // std::make_unique()
for (auto& i: t) {
// ...
}
// ERROR: error C3312: no callable 'begin' function found for type 'Thingy *'
// ERROR: error C3312: no callable 'end' function found for type 'Thingy *'
最佳答案
t
是一个 Thingy *
。您没有为 Thingy *
定义任何函数,您的函数是为 Thingy
定义的。
所以你必须写:
for (auto &i : *t)
关于c++ - 作为成员变量的 vector 上的基于范围的 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24195881/