template & passage de fonction en argument
Bonjour,
je viens de découvrir les templates et qu'on passer des fonctions en argument d'une fonction, mais je ne comprends pas encore bien comment cela fonctionne. Le mieux est que je montre ce que je cherche à faire :
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 32 33 34 35 36 37 38 39 40 41
| // fonction.h
template <typename T> void zone_brillouin(const int & P, T * in)
{
for(int i = 0; i<P; i++)
{
vector q=i;
in(q);
}
}
void Chi_c(const complex & iomega, const vector & q);
void Chi_0(const vector & q);
// fonction.cpp
void Chi_c(const complex & iomega, const vector & q)
{
}
void Chi_0(const vector & q)
{
}
// main.cpp
using namespace std;
#include "fonction.h"
#include "complex.h"
#include "vector.h"
int main()
{
complex C(0,1);
zone_brillouin(50,&Chi_0); // Fonctionne bien
zone_brillouin(50,&Chi_c); // Comment je peux définir les paramètres de gauches ?
return 0;
} |
À la compilation, j'ai ce type de message :
Code:
1 2 3 4 5 6 7 8 9 10
| $ make -j 5
In file included from fonctions.cpp:7:
fonctions.h: In function void zone_brillouin(const int&, T*) [with T = void(electron, const int&, const complex&, const vector&)]:
fonctions.cpp:183: instantiated from here
fonctions.h:35: error: conversion from vector to non-scalar type electron requested
fonctions.h:47: error: conversion from vector to non-scalar type electron requested
fonctions.h:63: error: conversion from vector to non-scalar type electron requested
fonctions.h:75: error: conversion from vector to non-scalar type electron requested
make: *** [fonctions.o] Erreur 1
$ |
(dans mon "vrai" programme, le premier argument de Chi_c est un electron)
J'ai environ 50 fonctions différentes que je vais passer en arguments à zone_brillouin, toutes avec un nombre différent d'arguments, où il faudrait que dans zone_brillouin, la fonction in agisse comme une fonction à une variable de type tector, les autres étant fixées dans le main.
Est ce que je suis clair ?
Si oui, cela est-il possible ?
Merci d'avance pou vos réponses.