문제는 다음과 같습니다. 다음 테스트에서는 엄청난 양의 컴파일러 오류가 발생합니다.템플릿 기능에서 벡터를 반환하려고하면 컴파일 오류가 발생합니다.
#include <vector>
using namespace std;
template<class T>
class test{
vector<T> vec;
public:
vector<T>::iterator begin();
};
template<class T>
vector<T>::iterator test<T>::begin(){
return vec.begin();
}
int main(){
test<float> testing;
testing.begin();
}
일부 컴파일러 오류 : 그러나
test.cpp(8): warning C4346: 'std::vector<T>::iterator' : dependent name is not a type
test.cpp(8): error C2146: syntax error : missing ';' before identifier 'begin'
test.cpp(13): error C2143: syntax error : missing ';' before 'test<T>::begin'
, 당신이 말에 대한 템플릿 vector<T>
을 교체하는 경우, vector<float>
의 컴파일 잘. 예 :
template<class T>
class test{
vector<T> vec;
public:
vector<float>::iterator begin();
};
template<class T>
vector<float>::iterator test<T>::begin(){
return vec.begin();
}
이유는 무엇입니까?
template<class T>
class test{
vector<T> vec;
public:
typename vector<T>::iterator begin();
};
template<class T>
typename vector<T>::iterator test<T>::begin(){
return vec.begin();
}
* * 멤버 * 멤버 변수가 아닌 * 멤버 * 유형 *입니다. –
@KerrekSB : 고침, 고마워. – jxh