菜鳥在這裏,錯誤:有「爲」沒有參數依賴模板參數,所以在申報必須是可用
我試圖從Bjarne的Stroustrup的「編譯這個代碼段C++編程語言「,但CodeBlocks不斷拋出這個錯誤。
該代碼是關於範圍檢查保存在向量函數中的數組。
下面是代碼:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
int i = 1000;
template<class T> class Vec : public vector<T>
{
public:
Vec() : vector<T>() { }
T& operator[] (int i) {return at(i); }
const T& operator[] (int i) const {return at(i); }
//The at() operation is a vector subscript operation
//that throws an exception of type out_of_range
//if its argument is out of the vector's range.
};
Vec<Entry> phone_book(1000);
int main()
{
return 0;
}
返回的錯誤是:
- 沒有參數「在」依賴模板參數,所以「在」必須的聲明可用
- 注意:(如果您使用'-fpermissive',G ++將接受您的代碼,但允許使用未聲明的名稱已棄用
- 在成員函數'const T &運算符[](int i)const':
- 'at'沒有依賴於模板參數的參數,因此必須提供'at'聲明
- 'Entry'未在此處聲明範圍
- 模板參數1是在之前的聲明無效
- 無效類型「(」令牌
有人可以解釋這樣對我?
另外,如果我不使用'using namespace std',我將如何實現這個功能?
從stl容器繼承時,應該意識到它們沒有虛擬析構函數。當您的子類獲取應該在其析構函數中釋放的資源時,這可能會導致問題。 – user463035818 2015-04-01 12:41:51
最後一個問題很簡單:在每個來自'namespace std'的名字前寫'std ::',例如在每個地方寫入'std :: vector',或者用'std :: vector'寫一次(而不是'namespace',只列出你使用的所有單個名字) – MSalters 2015-04-01 13:44:42