IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

bind méthode template


Sujet :

Langage C++

  1. #1
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 274
    Points : 176
    Points
    176
    Par défaut bind méthode template
    Bonjour à tous (eh oui encore moi )

    Cette fois je me heurte à un petit problème d'apparence assez simple mais que, malgré internet, je ne parviens pas à résoudre, la totalité des topics sur ce problème étant en anglais

    Je souhaite simplement binder une méthode d'une classe templatée (comme le titre du topic le suggère ), afin d'obtenir quelque chose comme cela :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    boost::bind(&Function<int,double,char>::getValue,&m1);
    Évidemment, j'ai essayé de nombreuses choses mais aucune ne compile. Je précise également que la classe est variadic template ce qui pourrait même si je n'y connais pas grand chose empêcher ce que je souhaite obtenir

  2. #2
    En attente de confirmation mail

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Août 2004
    Messages
    1 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 33
    Localisation : France, Doubs (Franche Comté)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 1 391
    Points : 3 311
    Points
    3 311
    Par défaut
    Bonjour,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    #include<functional>
     
    template<class>
    struct A
    {
    	void foo(int){}
    };
     
    int main()
    {
    	A<int> a;
    	std::bind(&A<int>::foo,a,0);
    };
    Compile.

    Tu pourrais poster un code reproduisant le problème ?

  3. #3
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 274
    Points : 176
    Points
    176
    Par défaut
    Ca ne compile toujours pas ^^
    Je pense que ça vient des template variadic enfin à confirmer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    template <typename T, typename ... Args>
    class Function
    {
        public:
            Function(Mecanism<T,Args...> &mec) : m_mec(mec) {}
            ~Function() {};
     
            T getValue(Args ... args)
            {
                if(m_mec.isDefined(args...))
                    return m_mec.tryGetValue(args...);
                else
                {
                    m_badValues.push_back(BadValues<Args...>(args...));
                    return 0;
                }
            }
     
        private:
            Mecanism<T,Args...> &m_mec;
            std::vector<BadValues<Args...> >m_badValues;
    };
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    CarreXYMecanism<int, double, char> m1;
        Function<int, double, char> f1(m1);
        boost::function<int(double,char)> func;
        func = boost::bind(&Function<int,double,char>::getValue,&m1,0); //ne compile pas

  4. #4
    Membre expert

    Avatar de germinolegrand
    Homme Profil pro
    Développeur de jeux vidéo
    Inscrit en
    Octobre 2010
    Messages
    738
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Développeur de jeux vidéo
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Octobre 2010
    Messages : 738
    Points : 3 892
    Points
    3 892
    Par défaut
    Il pourrait être fort utile d'avoir l'erreur que sort le compilateur... En général il parait que c'est marqué dedans

  5. #5
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 118
    Points : 158
    Points
    158
    Par défaut
    [EDIT]Ma réponse était incorrecte

  6. #6
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 274
    Points : 176
    Points
    176
    Par défaut
    En effet il doit s'agir de cela cependant comment résoudre le problème ?
    Voici le message d'erreur (il y en a pas mal du même type après mais ce sont les mêmes et ils portent sur la même ligne )
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/bind_template.hpp:15:48:   required from 'class boost::_bi::bind_t<boost::_bi::unspecified, int (Function<int, double, char>::*)(double, char), boost::_bi::list2<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::_bi::value<int> > >'
    D:\mes documents\SFML\SoundTemplateVariadic\main.cpp:57:66:   required from here
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/bind.hpp:69:37: error: 'int (Function<int, double, char>::*)(double, char)' is not a class, struct, or union type

  7. #7
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 118
    Points : 158
    Points
    158
    Par défaut
    Il semblerait que j'ai dit des bêtises dans mon précédent post.

    En faisant ça, ton code compile :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    func = boost::bind(&Function<int, double, char>::getValue, &f1, 0, _1);
    Cependant j'ai l'impression que tu voudrais plutot cela comme code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
        boost::function<int(char)> func;
        func = boost::bind(&Function<int, double, char>::getValue, &f1, 0, _1);
    En effet, comme tu bind le premier paramètre à la valeur 0 alors il te faut un boost::function ne prenant qu'un paramètre.

  8. #8
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 274
    Points : 176
    Points
    176
    Par défaut
    Oui en effet c'est déjà mieux
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    func = boost::bind(&Function<int,double,char>::getValue,&m1,_1,_2);
    Mais ça plante toujours cette fois l'endroit est différent (c'est toujours au niveau de la même ligne de code mais pas au même niveau des instructions de boost) :
    Dans mem_fn_template.hpp :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    BOOST_MEM_FN_RETURN call(u, p, a1, a2);
    Ca plante ici avec le message :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/mem_fn_template.hpp: In instantiation of 'R boost::_mfi::mf2<R, T, A1, A2>::call(U&, const void*, B1&, B2&) const [with U = CarreXYMecanism<int, double, char>*; B1 = double; B2 = char; R = int; T = Function<int, double, char>; A1 = double; A2 = char]':
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/mem_fn_template.hpp:286:46:   required from 'R boost::_mfi::mf2<R, T, A1, A2>::operator()(U&, A1, A2) const [with U = CarreXYMecanism<int, double, char>*; R = int; T = Function<int, double, char>; A1 = double; A2 = char]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/bind.hpp:382:98:   required from 'R boost::_bi::list3<A1, A2, A3>::operator()(boost::_bi::type<R>, F&, A&, long int) [with R = int; F = boost::_mfi::mf2<int, Function<int, double, char>, double, char>; A = boost::_bi::list2<double&, char&>; A1 = boost::_bi::value<CarreXYMecanism<int, double, char>*>; A2 = boost::arg<1>; A3 = boost::arg<2>]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/bind_template.hpp:61:59:   required from 'boost::_bi::bind_t<R, F, L>::result_type boost::_bi::bind_t<R, F, L>::operator()(A1&, A2&) [with A1 = double; A2 = char; R = int; F = boost::_mfi::mf2<int, Function<int, double, char>, double, char>; L = boost::_bi::list3<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::arg<1>, boost::arg<2> >; boost::_bi::bind_t<R, F, L>::result_type = int]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/function/function_template.hpp:132:42:   required from 'static R boost::detail::function::function_obj_invoker2<FunctionObj, R, T0, T1>::invoke(boost::detail::function::function_buffer&, T0, T1) [with FunctionObj = boost::_bi::bind_t<int, boost::_mfi::mf2<int, Function<int, double, char>, double, char>, boost::_bi::list3<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::arg<1>, boost::arg<2> > >; R = int; T0 = double; T1 = char]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/function/function_template.hpp:913:60:   required from 'void boost::function2<R, T1, T2>::assign_to(Functor) [with Functor = boost::_bi::bind_t<int, boost::_mfi::mf2<int, Function<int, double, char>, double, char>, boost::_bi::list3<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::arg<1>, boost::arg<2> > >; R = int; T0 = double; T1 = char]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/function/function_template.hpp:722:7:   required from 'boost::function2<R, T1, T2>::function2(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<int, boost::_mfi::mf2<int, Function<int, double, char>, double, char>, boost::_bi::list3<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::arg<1>, boost::arg<2> > >; R = int; T0 = double; T1 = char; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/function/function_template.hpp:1064:16:   required from 'boost::function<R(T0, T1)>::function(Functor, typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type) [with Functor = boost::_bi::bind_t<int, boost::_mfi::mf2<int, Function<int, double, char>, double, char>, boost::_bi::list3<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::arg<1>, boost::arg<2> > >; R = int; T0 = double; T1 = char; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, int>::type = int]'
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/function/function_template.hpp:1105:5:   required from 'typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R(T0, T1)>&>::type boost::function<R(T0, T1)>::operator=(Functor) [with Functor = boost::_bi::bind_t<int, boost::_mfi::mf2<int, Function<int, double, char>, double, char>, boost::_bi::list3<boost::_bi::value<CarreXYMecanism<int, double, char>*>, boost::arg<1>, boost::arg<2> > >; R = int; T0 = double; T1 = char; typename boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value, boost::function<R(T0, T1)>&>::type = boost::function<int(double, char)>&]'
    D:\mes documents\SFML\SoundTemplateVariadic\main.cpp:57:70:   required from here
    c:\mingw_\mingw\bin\../lib/gcc/mingw32/4.7.0/../../../../include/boost/bind/mem_fn_template.hpp:271:57: error: pointer to member type 'int (Function<int, double, char>::)(double, char)' incompatible with object type 'CarreXYMecanism<int, double, char>'

  9. #9
    Membre habitué
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    118
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 118
    Points : 158
    Points
    158
    Par défaut
    tu as oublié, je pense, de modifier &m1 par &f1.

  10. #10
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Avril 2011
    Messages
    274
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 274
    Points : 176
    Points
    176
    Par défaut
    Tu as raison décidément j'accumule les erreurs en ce moment

    Merci à toi sujet résolu ^^

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Bind d'une méthode template privée
    Par kidpaddle2 dans le forum Boost
    Réponses: 2
    Dernier message: 29/06/2010, 16h32
  2. Méthode template : déclaration/définition et appel ?
    Par kidpaddle2 dans le forum Langage
    Réponses: 3
    Dernier message: 22/04/2007, 14h09
  3. Méthode template dans classe non template ?
    Par shenron666 dans le forum Langage
    Réponses: 12
    Dernier message: 04/09/2006, 17h50
  4. Appeler une méthode template d'un paramètre template
    Par YéTeeh dans le forum Langage
    Réponses: 2
    Dernier message: 26/08/2006, 12h50
  5. Méthode template virtuelle
    Par ced1 dans le forum Langage
    Réponses: 6
    Dernier message: 13/11/2005, 15h14

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo