C++
x
26
1
2
3
template <class T> class MyVector {
4
private:
5
std::vector<T> m_vector;
6
public:
7
void add(T value) {
8
m_vector.push_back(value);
9
}
10
std::vector<T>::iterator begin() { // <==== エラー!!
11
return m_vector.begin();
12
}
13
std::vector<T>::iterator end() { // <==== エラー!!
14
return m_vector.end();
15
}
16
};
17
18
int main() {
19
MyVector<int> values;
20
values.add(1);
21
values.add(2);
22
for (auto it=values.begin(); it!=values.end(); it++) {
23
// 反復
24
}
25
return 0;
26
}
$ g++ prog.cc -Wall -Wextra -std=c++11 -pedantic
Start
prog.cc:10:3: error: need 'typename' before 'std::vector<T>::iterator' because 'std::vector<T>' is a dependent scope 10 | std::vector<T>::iterator begin() { // <==== エラー!! | ^~~ | typename prog.cc:13:3: error: need 'typename' before 'std::vector<T>::iterator' because 'std::vector<T>' is a dependent scope 13 | std::vector<T>::iterator end() { // <==== エラー!! | ^~~ | typename prog.cc: In function 'int main()': prog.cc:22:23: error: 'class MyVector<int>' has no member named 'begin' 22 | for (auto it=values.begin(); it!=values.end(); it++) { | ^~~~~ prog.cc:22:43: error: 'class MyVector<int>' has no member named 'end' 22 | for (auto it=values.begin(); it!=values.end(); it++) { | ^~~
1
Finish