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

C++ Discussion :

Pointeur sur fonction membre avec parametre


Sujet :

C++

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    58
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 58
    Points : 40
    Points
    40
    Par défaut Pointeur sur fonction membre avec parametre
    Bonjour, alors voila j'ai envie d'appeler une fonction d'une classe a un autre sans pour autant savoir laquel je vais appeler d'avance (gestion d'evenement en gros)

    Donc si j'ai ce code la par exemple :

    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
    23
    24
    25
    26
     
    class Truc
    {
      boost&#58;&#58;function<void &#40;&#41;> event;
     
      void set_event&#40;boost&#58;&#58;function<void &#40;&#41;> e&#41;
      &#123;
         event = e;
      &#125;
     
      // Une fonction quelconque qui appel event
    &#125;;
     
    class Machin&#123;
      Truc t;
      void appel&#40;&#41;
      &#123;
        cout << test << endl;
      &#125;
     
      Machin&#40;&#41;
      &#123;
        t.set_event&#40;bind&#40;&Machin&#58;&#58;appel, boost&#58;&#58;ref&#40;*this&#41;&#41;;
      &#125;
     
    &#125;;
    Ca ca marche (enfin j'ai pas testé ce code que je viens de faire expret pour l'exemple).
    Mais si je veux passer 1 parametre, la je n'y arrive plus du tout.

    J'ai deja epuré le forum et le site de boost mais pas moyen de trouver un point de depart.

    Merci d'avance

  2. #2
    Membre éclairé Avatar de MatRem
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    750
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 750
    Points : 693
    Points
    693
    Par défaut
    J'ai pas trop compris ton code.
    Tu dois utiliser boost que je ne connais pas.

    mais si tu veux des infos sur les pointeurs de fontions membres, voilà deux adresses intéressantes:
    la faq c++ de développez.com
    Les cours et tutoriels pour apprendre le c++

  3. #3
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Mais si je veux passer 1 parametre, la je n'y arrive plus du tout.
    Montre nous plutôt ce que tu as codé et les erreurs que cela génère.

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    58
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 58
    Points : 40
    Points
    40
    Par défaut
    Ok et bien voici la partie du code qui pose probleme

    j'ai mis les numéros de ligne pour reperer les lignes d'erreurs

    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
    23
    24
     
    48&#58;          boost&#58;&#58;function<void &#40;&#41;> get_event_mouse_func&#40;&#41;;
    49&#58;          boost&#58;&#58;function<void &#40;&#41;&#40;int&#41;> get_event_keyboard_func&#40;&#41;;
     
     
    void Menu&#58;&#58;event_mouse&#40;&#41;
    &#123;
            cout << "Test souris" << endl;
    &#125;
     
    void Menu&#58;&#58;event_keyboard&#40;int key&#41;
    &#123;
            cout << &#40;int&#41;key << " ; " << &#40;char&#41;key << endl;
    &#125;
     
    boost&#58;&#58;function<void &#40;&#41;> Menu&#58;&#58;get_event_mouse_func&#40;&#41;
    &#123;
            return boost&#58;&#58;bind&#40;&Menu&#58;&#58;event_mouse, boost&#58;&#58;ref&#40;*this&#41;&#41;;
    &#125;
     
    107&#58; boost&#58;&#58;function<void &#40;&#41;&#40;int&#41;> Menu&#58;&#58;get_event_keyboard_func&#40;&#41;
    108&#58; &#123;
    109&#58;        return boost&#58;&#58;bind&#40;&Menu&#58;&#58;event_keyboard, boost&#58;&#58;ref&#40;*this&#41;&#41;;
    110&#58; &#125;
    Et voila les quelques erreurs

    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
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
     
    Menu.hpp&#58;49&#58; erreur&#58; ‘type name’ declared as function returning a function
    Menu.hpp&#58;49&#58; erreur&#58; ‘type name’ declared as function returning a function
    Menu.hpp&#58;49&#58; erreur&#58; ‘type name’ declared as function returning a function
    Menu.cpp&#58;107&#58; erreur&#58; ‘type name’ declared as function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58; In instantiation of ‘boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >’&#58;
    Menu.cpp&#58;109&#58;   instantiated from here
    /usr/include/boost/bind/bind_template.hpp&#58;17&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;23&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;29&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;35&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;41&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;47&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;53&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;59&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;65&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;71&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;77&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;83&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;89&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;95&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;101&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;107&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;113&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;119&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;125&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;131&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;137&#58; erreur&#58; function returning a function
    /usr/include/boost/bind/bind_template.hpp&#58;142&#58; erreur&#58; function returning a function
    /usr/include/boost/function/function_template.hpp&#58; In static member function ‘static R boost&#58;&#58;detail&#58;&#58;function&#58;&#58;function_obj_invoker0<FunctionObj, R>&#58;&#58;invoke&#40;boost&#58;&#58;detail&#58;&#58;function&#58;&#58;any_pointer&#41; &#91;with FunctionObj = boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >, R = int&#93;’&#58;
    /usr/include/boost/function/function_template.hpp&#58;479&#58;   instantiated from ‘void boost&#58;&#58;function0<R, Allocator>&#58;&#58;assign_to&#40;FunctionObj, boost&#58;&#58;detail&#58;&#58;function&#58;&#58;function_obj_tag&#41; &#91;with FunctionObj = boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >, R = int, Allocator = std&#58;&#58;allocator<void>&#93;’
    /usr/include/boost/function/function_template.hpp&#58;430&#58;   instantiated from ‘void boost&#58;&#58;function0<R, Allocator>&#58;&#58;assign_to&#40;Functor&#41; &#91;with Functor = boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >, R = int, Allocator = std&#58;&#58;allocator<void>&#93;’
    /usr/include/boost/function/function_template.hpp&#58;294&#58;   instantiated from ‘boost&#58;&#58;function0<R, Allocator>&#58;&#58;function0&#40;Functor, typename boost&#58;&#58;enable_if_c<boost&#58;&#58;type_traits&#58;&#58;ice_not<boost&#58;&#58;is_integral<Functor>&#58;&#58;value>&#58;&#58;value, int>&#58;&#58;type&#41; &#91;with Functor = boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >, R = int, Allocator = std&#58;&#58;allocator<void>&#93;’
    /usr/include/boost/function/function_template.hpp&#58;637&#58;   instantiated from ‘boost&#58;&#58;function<R &#40;&#41;&#40;&#41;, Allocator>&#58;&#58;function&#40;Functor, typename boost&#58;&#58;enable_if_c<boost&#58;&#58;type_traits&#58;&#58;ice_not<boost&#58;&#58;is_integral<Functor>&#58;&#58;value>&#58;&#58;value, int>&#58;&#58;type&#41; &#91;with Functor = boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >, R = int, Allocator = std&#58;&#58;allocator<void>&#93;’
    Menu.cpp&#58;109&#58;   instantiated from here
    /usr/include/boost/function/function_template.hpp&#58;119&#58; erreur&#58; no match for call to ‘&#40;boost&#58;&#58;_bi&#58;&#58;bind_t<void &#40;&#41;&#40;int&#41;, boost&#58;&#58;_mfi&#58;&#58;dm<void &#40;&#41;&#40;int&#41;, Menu>, boost&#58;&#58;_bi&#58;&#58;list1<boost&#58;&#58;reference_wrapper<Menu> > >&#41; &#40;&#41;’
    !^&#91;&#91;Dmake&#58; *** &#91;all&#93; Erreur 1
    J'ai oublié de dire, mais vous l'avez sans doute remarqué, que je programme sous linux

    Voila voila [/code]

  5. #5
    Membre éclairé Avatar de MatRem
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    750
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 750
    Points : 693
    Points
    693
    Par défaut
    N'est pas des pointeurs de fonctions que tu veux utiliser ici:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    48&#58;          boost&#58;&#58;function<void &#40;&#41;> get_event_mouse_func&#40;&#41;;
    49&#58;          boost&#58;&#58;function<void &#40;&#41;&#40;int&#41;> get_event_keyboard_func&#40;&#41;;
    J'aurais plutôt fait ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    48&#58;          boost&#58;&#58;function<void &#40;*&#41;&#40;void&#41;> get_event_mouse_func&#40;&#41;;
    49&#58;          boost&#58;&#58;function<void &#40;*&#41;&#40;int&#41;> get_event_keyboard_func&#40;&#41;;
    Je suis plus sur à 100% que c'est la bonne syntaxe...

  6. #6
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2002
    Messages
    58
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2002
    Messages : 58
    Points : 40
    Points
    40
    Par défaut
    Non j'avais deja essayé cette syntaxe et ca marche pas

  7. #7
    Membre averti Avatar de Rafy
    Profil pro
    Inscrit en
    Juillet 2005
    Messages
    415
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France

    Informations forums :
    Inscription : Juillet 2005
    Messages : 415
    Points : 417
    Points
    417
    Par défaut
    Avec boost les pointeur de fonction c'est un peu compliqué...
    Tu as déjà fait sans utiliser boost ?
    Première grosse démo en construction :
    http://bitbucket.org/rafy/exo2/

  8. #8
    Rédacteur/Modérateur
    Avatar de JolyLoic
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2004
    Messages
    5 463
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 49
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2004
    Messages : 5 463
    Points : 16 213
    Points
    16 213
    Par défaut
    Le premier est un foncteur ne prenant pas d'arguments, et ne retournant rien.

    Le deuxième, je ne sais pas ce que tu as voulu mettre...

    Si tu veux un foncteur prenant un int et ne retournant rien, écrit simplement :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    boost&#58;&#58;function<void &#40;int&#41;>
    Ma session aux Microsoft TechDays 2013 : Développer en natif avec C++11.
    Celle des Microsoft TechDays 2014 : Bonnes pratiques pour apprivoiser le C++11 avec Visual C++
    Et celle des Microsoft TechDays 2015 : Visual C++ 2015 : voyage à la découverte d'un nouveau monde
    Je donne des formations au C++ en entreprise, n'hésitez pas à me contacter.

Discussions similaires

  1. Réponses: 3
    Dernier message: 20/02/2008, 17h29
  2. pbm pointeurs sur fonction membre :)
    Par overbreak dans le forum C++
    Réponses: 8
    Dernier message: 15/02/2008, 16h14
  3. [POO] Pointeur sur fonction membre et héritage
    Par MrDuChnok dans le forum C++
    Réponses: 9
    Dernier message: 20/07/2006, 18h19
  4. Réponses: 10
    Dernier message: 03/02/2005, 14h09
  5. Réponses: 5
    Dernier message: 12/01/2005, 21h58

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