我試圖使用模板來封裝C++標準庫的向量類,但我不斷收到錯誤C++模板包裝類的std :: vector的
SceneVector.h: In member function ‘void scenegraph::SceneVector<V>::print()’:
SceneVector.h:40: error: expected ‘;’ before ‘it’
SceneVector.h:40: error: ‘it’ was not declared in this scope
我已經成功地創建的代碼是
#include <map>
#include <vector>
#include <iostream>
namespace scenegraph
{
template <class V> class SceneVector
{
typedef std::vector<V> Vector;
Vector vector;
public:
SceneVector();
void insert(const V value);
void print();
};
template <class V> SceneVector<V>::SceneVector()
{
vector.clear();
}
template <class V> void SceneVector<V>::insert(const V value)
{
vector.push_back(value);
}
template <class V> void SceneVector<V>::print()
{
for(Vector::iterator it = vector.begin(); it != vector.end(); ++it)
{
std::cout << "[" << (*it) << "] " << std::endl;
}
std::cout << std::endl;
}
}
任何人都可以在這裏糾正我嗎?我必須強化我是C++新手,所以答案可能非常微不足道。
另一個......你需要'typename'。 http://stackoverflow.com/q/1123080/51831 – jpalecek
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.21 – ephemient