Hello,
un exemple qui marche:
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
namespace A1{
namespace A2{
class Voiture{
public:
std::string toString(){return "A-Voiture";}
};
class Accident{
public:
int getFee()const{return 0;}
};
//...
class Definition{
public:
typedef A1::A2::Voiture MyVoiture;
typedef A1::A2::Accident MyAccident;
};
}
}
namespace B1{
namespace B2{
class Voiture{
public:
std::string toString(){return "B-voiture";}
};
class Accident{
public:
int getFee()const{return 2;}
};
class Definition{
public:
typedef B1::B2::Voiture MyVoiture;
typedef B1::B2::Accident MyAccident;
};
}
}
template<typename Def>
void registerCar(const typename Def::MyAccident& iAccident){
typename Def::MyVoiture aVoiture;
std::cout<<aVoiture.toString()<<":"<<iAccident.getFee()<<std::endl;
}
int main(){
A1::A2::Definition::MyAccident accidentA;
registerCar<A1::A2::Definition>(accidentA);
B1::B2::Definition::MyAccident accidentB;
registerCar<B1::B2::Definition>(accidentB);
/*
Affiche:
A-Voiture:0
B-voiture:2
*/
return 0;
} |
L'arborescence des classes est imposée.
Mon bras de levier c'est la méthode registerCar, qui prend un accident.
Mon but c'est de trouver la voiture dans le même namespace que l'accident qui lui est passé en paramètre.
J'aimerais ne pas avoir besoin de préciser registerCar<Type> mais simplement faire un appel registerCar(accidentA); étant donné que accidentA contient le type A1::A2::... j'aimerais pouvoir me servir de accidentA pour pouvoir utiliser A1::A2::MyVoiture
Y a-t-il un moyen?
1 2 3 4 5
| template<typename T, typename U>
void registerCar(const typename T::U& iAccident){
typename T::MyVoiture aVoiture;
std::cout<<aVoiture.toString()<<":"<<iAccident.getFee()<<std::endl;
} |
ne marche pas
src/main.cpp:46:23: erreur: no matching function for call to ‘registerCar(A1::A2::Definition::MyAccident&)’
src/main.cpp:46:23: note: candidate is:
src/main.cpp:39:6: note: template<class T, class U> void registerCar(const typename T::U&)
1 2 3 4 5
| template<typename T>
void registerCar(const typename T::MyAccident& iAccident){
typename T::MyVoiture aVoiture;
std::cout<<aVoiture.toString()<<":"<<iAccident.getFee()<<std::endl;
} |
non plus
src/main.cpp: In function ‘int main()’:
src/main.cpp:46:23: erreur: no matching function for call to ‘registerCar(A1::A2::Definition::MyAccident&)’
src/main.cpp:46:23: note: candidate is:
src/main.cpp:39:6: note: template<class T> void registerCar(const typename T::MyAccident&)
Merci !
Partager