g ++編譯器給出了這個錯誤:期望';' '之前'C++ list <T> ::迭代器不能用於派生類模板
template <typename T>
class myList : public std::list<T>
{
public:
void foo()
{
std::list<T>::iterator it; // compiler error as above mentioned, why ???
}
};
謝謝。
g ++編譯器給出了這個錯誤:期望';' '之前'C++ list <T> ::迭代器不能用於派生類模板
template <typename T>
class myList : public std::list<T>
{
public:
void foo()
{
std::list<T>::iterator it; // compiler error as above mentioned, why ???
}
};
謝謝。
In g ++。每當在模板中,你看到的錯誤:
error: expected ';' before 'it'
懷疑你需要一個類型名稱:
typename std::list<T>::iterator it;
這在將模板聲明瞭一個新的類型(在這種情況下,列表迭代器)需要這取決於一個或多個模板參數。 g ++ BTW並不是唯一的,它是標準C++的一部分。
尼爾給你你的答案。這就是說,你可能想要做的typedef的的轉換和使用這些,所以你的工作不會變得單調乏味(而且還會增加可讀性):
template <typename T>
class myList : public std::list<T>
{
public:
typedef T value_type;
typedef const T const_value_type;
typedef value_type& reference;
typedef const_value_type& const_reference;
typedef value_type* pointer;
typedef const_value_type* const_pointer;
typedef std::list<T> base_container;
typedef typename base_container::iterator iterator;
typedef typename base_container::const_iterator const_iterator;
void foo()
{
iterator it; // easy peasy
}
};
是很常見的是自由主義與您使用的typedef的的。
另外,從標準容器繼承可能是一個壞主意,因爲它們並不是真正爲這樣的事情做準備。例如,如果您正在尋找容器的一些擴展,則免費功能通常是best choice。
Thanx。這確實讓我有了更多的見解。 – 2010-03-29 17:12:52
感謝這篇不錯的小文章,它幫助了我很多! – 2014-01-01 17:06:58
@anon:通過更改來獲取類型名是強制性的案例列表。乾杯(+1) – ezdazuzena 2013-03-04 13:31:48