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 :

Problème de passage de paramètres à une fonction template


Sujet :

C++

  1. #1
    Invité
    Invité(e)
    Par défaut Problème de passage de paramètres à une fonction template
    Salut je possède une classe avec des fonction template comme ceci :
    Code cpp : 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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
     
    #ifndef UPS_State_PARAMETER_H
    #define UPS_State_PARAMETER_H
    #include "any.h"
    #include <string>
    /**
     *\namespace odfaeg
     * the namespace of the Opensource Development Framework Adapted for Every Games.
     */
    namespace odfaeg {
        namespace core {
            /**
            * \file stateParameter.h
            * \class StateManager
            * \brief A parameter, this class is used to set parameters to the states.
            * \author Duroisin.L
            * \version 1.0
            * \date 1/02/2014
            */
            class ODFAEG_CORE_API StateParameter {
     
                private :
                    any value; /**> the value of the parameter. (can be of any type)*/
                    std::string name; /**> the name of the parameter.*/
     
                public :
                    /** \fn StateParameter (T value, std::string name)
                    *    \brief constructor.
                    *    \param T value : the value of the parameter.
                    *    \param std::string name : the name of the parameter.
                    */
                    template <typename T> StateParameter (T value, std::string name) :
                         name (name), value (std::forward<T>(value)) {
                    }
                    /**  \fn T getValue ()
                    *    \brief get the value of the parameter.
                    *    \return T : the value of the parameter.
                    */
                    template <typename T> T getValue () const {
                        return value.get<T>();
                    }
                    /**  \fn void setValue (T value)
                    *    \brief set the value of a parameter.
                    *    \return the value of the parameter.
                    */
                    template <typename T>  void setValue (T value) {
                        value.set (std::forward<T>(value));
                    }
                    /** \fn std::string getName() const
                    *   \return the name of the parameter.
                    */
                    std::string getName() const {
                        return name;
                    }
                    /** \fn void setName()
                    *   \brief std::string name : set the name of a parameter.
                    *   \param std::string name : the name of the parameter.
                    */
                    void setName(std::string name) {
                        this->name = name;
                    }
                    /** \fn ~StateParameter()
                    *   \brief destructor.
                    */
                    ~StateParameter() {
     
                    }
            };
        }
    }
    #endif
    Une autre classe qui contient tout les paramètres :
    Code cpp : 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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
     
    #ifndef State_H
    #define State_H
    #include <vector>
    #include "erreur.h"
    #include <sstream>
    #include "stateParameter.h"
    #include <memory>
    /**
     *\namespace odfaeg
     * the namespace of the Opensource Development Framework Adapted for Every Games.
     */
    namespace odfaeg {
            namespace core {
            class StateExecutor;
            /**
            * \file state.h
            * \class State
            * \brief holds a particular state of the application. (Or of an entity)
            * \author Duroisin.L
            * \version 1.0
            * \date 1/02/2014
            */
            class ODFAEG_CORE_API State {
     
     
                private :
                    std::string name; /**> std::string name the name of the state*/
                    StateExecutor* stateExec; /**> a state executor to apply and unapply the state.*/
                    /** \fn State& operator= (const State &p);
                    *   \brief affector
                    *   \param const State &p : affect the current state.
                    *   \return State& the affected state.
                    */
                    std::vector<StateParameter*> parameters; /**> parameters of the states.*/
                    static int nbStates; /**> nb states created. */
                public :
                    /**\fn  State (std::string name, StateExecutor *stateExec);
                    *  \brief create a state with the given state executor which apply and unapply the state.
                    *  \param std::string name : the name of the state.
                    *  \param StateExecutor stateExec : a pointer to a StateExecutor which apply and unapply a state.
                    */
                    State (std::string name, StateExecutor *stateExec);
                    std::string getName () const;
                    void setName (std::string name);
                    template <typename T> bool addParameter (std::string name, T value) {
     
                        StateParameter* p = new StateParameter (value, name);
     
                        for (unsigned int i = 0; i < parameters.size(); i++) {
                            if (parameters[i]->getName() == name) {
                                delete p;
                                return false;
                            }
                        }
     
                        parameters.push_back(p);
                        return true;
                    }
                    /**\fn  bool removeParameter (std::string name)
                    *  \brief remove a parameter to the state.
                    *  \param std::string name : the name of the parameter.
                    */
                    bool removeParameter (std::string name);
                    StateParameter& getParameter (const std::string name) throw (Erreur) {
                            for (unsigned int i = 0; i < parameters.size(); i++) {
                                if (parameters[i]->getName() == name) {
     
                                    return *parameters[i];
                                }
                            }
                            throw Erreur(5, "No such parameter for this State : " + name, 2);
                    }
                    /**\fn  bool changeParameter (std::string name, T* value)
                    *  \brief change the value of a parameter.
                    *  \param std::string name : the name of the parameter.
                    *  \param T* value : the value of the parameter.
                    */
                    template <typename T> void changeParameter (const std::string name, T value) {
                        StateParameter& parameter = getParameter(name);
                        parameter.setValue(value);
                    }
                    /**\fn void setExecutor (StateExecutor *exec)
                    *  \brief set the executor to apply or unapply the state.
                    *  \param StateExecutor *exec : the executor.
                    */
                    void setExecutor (StateExecutor* exec) {
                        this->stateExec = exec;
                    }
                    /**\fn bool doActionState ()
                    *  \brief apply the state.
                    *  \return true if the state has been correctly applied.
                    */
                    bool doActionState ();
                    /**\fn bool undoActionState ()
                    *  \brief unapply the state.
                    *  \return true if the state has been correctly applied.
                    */
                    bool undoActionState ();
                    static int getNbStates () {
                        return nbStates;
                    }
                    /**\fn ~State()
                    *  \brief destructor.
                    */
                    ~State () {
                        for (unsigned int i = 0; i < parameters.size(); i++) {
                            delete parameters[i];
                        }
                        parameters.clear();
                        nbStates--;
                    }
            };
        }
    }
    #endif

    Et la classe Entity pour ajouter des paramètres perso aux entités :
    Code cpp : 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
     
    /** \fn void addAttribute (std::string name, T value)
                    *   \brief add an attribute to the entity.
                    *   \param std::string the name of the attribute.
                    *   \param T the value of the attribute.
                    *   \return if the attribute has been correctly added.
                    */
                    template <typename T> bool addAttribute (std::string name, T value) {
                        return entityState.addParameter(name, value);
                    }
                    /** \fn const StateParameter& getAttribute (const std::string name)
                    *   \brief get an attribute of the entity.
                    *   \param std::string name : the name of the attribute of the entity.
                    *   \return StateParameter& : the attribute of the entity.
                    */
                    const core::StateParameter& getAttribute (const std::string name) throw (core::Erreur) {
                           return entityState.getParameter(name);
                    }
                    /** \fn void changeAttribute (const std::string name)
                    *   \brief change the value of the attribute of the entity.
                    *   \param std::string name : the name of the attribute of the entity.
                    *   \param T value : the value of the attribute.
                    */
                    template <typename T> void changeAttribute (const std::string name, T value) {
                        entityState.changeParameter(name, value);
                    }

    Le problème est lorsque j'appelle la méthode changeAttribute ici :
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    if (!addAttribute("isAlive",Application::app->getClock("TimeClock").getElapsedTime().asMicroseconds()))
                    changeAttribute("isAlive",Application::app->getClock("TimeClock").getElapsedTime().asMicroseconds());

    Il me met cette erreur-ci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    ||=== Build: Debug in ODFAEG-CLIENT (compiler: GNU GCC Compiler) ===|
    /home/laurent/Développement/Projets-c++/ODFAEG-CLIENT/application.cpp|415|warning: "/*" within comment [-Wcomment]|
    /home/laurent/Développement/Projets-c++/ODFAEG-CLIENT/application.cpp||In member function ‘virtual void sorrok::MyAppli::onExec()’:|
    /home/laurent/Développement/Projets-c++/ODFAEG-CLIENT/application.cpp|640|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
    /home/laurent/Développement/Projets-c++/ODFAEG-CLIENT/application.cpp|759|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
    /home/laurent/Développement/Projets-c++/ODFAEG-CLIENT/application.cpp|797|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|
    /usr/local/include/odfaeg/Core/stateParameter.h||In instantiation of ‘void odfaeg::core::StateParameter::setValue(T) [with T = long long int]’:|
    /usr/local/include/odfaeg/Core/state.h|80|required from ‘void odfaeg::core::State::changeParameter(std::string, T) [with T = long long int; std::string = std::basic_string<char>]’|
    /usr/local/include/odfaeg/Graphics/entity.h|253|required from ‘void odfaeg::graphic::Entity::changeAttribute(std::string, T) [with T = long long int; std::string = std::basic_string<char>]’|
    /home/laurent/Développement/Projets-c++/ODFAEG-CLIENT/caracter.cpp|107|required from here|
    /usr/local/include/odfaeg/Core/stateParameter.h|46|error: request for member ‘set’ in ‘value’, which is of non-class type ‘long long int’|
    ||=== Build failed: 1 error(s), 8 warning(s) (0 minute(s), 10 second(s)) ===|
    Je ne comprend pas très bien cette erreur : request for member 'set' in 'value', which is of non-class type 'long long int'|

    Requis pour membre set dans value qui n'est pas de type long long int ?

    Pourtant le paramètre "isAlive" est de type sf::Int64 qui est un long long int.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Ha merde ces deux là ont le même nom il fallait mettre un this :
    Code cpp : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    template <typename T>  void setValue (T value) {
                        this->value.set (std::forward<T>(value));
                    }

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

Discussions similaires

  1. Réponses: 11
    Dernier message: 07/06/2013, 10h06
  2. Réponses: 5
    Dernier message: 01/03/2007, 17h19
  3. Passage de paramètre à une fonction dans un G_CALLBACK
    Par Dimitri_87 dans le forum GTK+ avec C & C++
    Réponses: 5
    Dernier message: 15/09/2006, 11h04
  4. [PL/SQL] : Passage de paramètres à une fonction
    Par dcollart dans le forum Oracle
    Réponses: 5
    Dernier message: 13/07/2006, 10h17
  5. Problème de passage de paramètres à une procedure
    Par momo62 dans le forum x86 16-bits
    Réponses: 2
    Dernier message: 22/12/2005, 15h22

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