問題如下:以下測試會引發大量編譯器錯誤。試圖從模板函數返回一個向量會引發編譯錯誤。
#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();
}
任何想法爲什麼?
它*是*成員 - 只是成員*類型*,不是成員變量。 –
@KerrekSB:更正,謝謝。 – jxh