1 pièce(s) jointe(s)
Templates imbriqués, problème avec vector::iterator
Bonjour,
J'ai un petit problème avec un code c++. J'ai une classe "StepFunction<T>" qui contient en gros un "vector" de "Point<T>" triés. Pour faire une insertion, j'ai besoin d'un itérateur dont le type serait
Code:
vector<Point<T> >::iterator it
Problème : j'ai une erreur à la compilation :
Citation:
test.cpp:22: error: expected `;' before ‘it’
test.cpp:23: error: ‘it’ was not declared in this scope
Je peux pourtant déclarer sans erreur
Code:
vector<Point<T> > v
ou
Code:
vector<Point<int> >::iterator it
Voici mon code complet (très simplifié, bien sûr):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <vector>
using namespace std;
template <class T>
class Point{
public:
Point(double time, T value):time(time),value(value){}
double time;
T value;
};
template <typename T>
class StepFunction{
public:
StepFunction(){}
vector<Point<T> > points;
void addPoint(Point<T> p);
};
template <typename T> void StepFunction<T>::addPoint(Point<T> p){
vector<Point<T> >::iterator it;
for(it = points.begin(); it != points.end() && (*it).getTime()<p.getTime(); ++it)
;
points.insert(it, Point<T>(p));
}
int main(int argc, char **argv){
StepFunction<int> sf;
sf.addPoint(Point<int>(0,5));
} |
Quelqu'un a une idée ?
Je parviens à compiler mon code en "instanciant" ma fonction "addPoint" comme ceci, mais ce n'est bien sûr pas très satisfaisant :
Code:
1 2 3 4 5 6 7
|
template <> void StepFunction<int>::addPoint(Point<int> p){
vector<Point<int> >::iterator it;
for(it = points.begin(); it != points.end() && (*it).time<p.time; ++it)
;
points.insert(it, Point<int>(p));
} |
Merci d'avance.