我试图使用模板来封装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