我一直在試圖定義一個使用在類命名空間中聲明的返回類型的類方法:C++「......沒有指定類型」
template<class T, int SIZE>
class SomeList{
public:
class SomeListIterator{
//...
};
using iterator = SomeListIterator;
iterator begin() const;
};
template<class T, int SIZE>
iterator SomeList<T,SIZE>::begin() const {
//...
}
當我嘗試編譯代碼,我得到這個錯誤:
Building file: ../SomeList.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"SomeList.d" -MT"SomeList.d" -o "SomeList.o" "../SomeList.cpp"
../SomeList.cpp:17:1: error: ‘iterator’ does not name a type
iterator SomeList<T,SIZE>::begin() const {
^
make: *** [SomeList.o] Error 1
我也試過這樣定義的方法:
template<class T, int SIZE>
SomeList::iterator SomeList<T,SIZE>::begin() const {
//...
}
這:
template<class T, int SIZE>
SomeList<T,SIZE>::iterator SomeList<T,SIZE>::begin() const {
//...
}
結果:
Building file: ../SomeList.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"SomeList.d" -MT"SomeList.d" -o "SomeList.o" "../SomeList.cpp"
../SomeList.cpp:17:1: error: invalid use of template-name ‘SomeList’ without an argument list
SomeList::iterator SomeList<T,SIZE>::begin() const {
^
make: *** [SomeList.o] Error 1
我在做什麼錯?
'SomeList :: iterator SomeList :: begin()const {' –
Lol4t0