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 avec l'article sur la meta-programmation


Sujet :

C++

  1. #1
    Futur Membre du Club
    Inscrit en
    Juin 2008
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 9
    Points : 8
    Points
    8
    Par défaut Problème avec l'article sur la meta-programmation
    Bonsoir,
    je suis actuellement en train de m'interesser au sujet de la "Métaprogrammation" (la lecture de l'article sur le Yes Engine m'a fait découvrir ce sujet :p) et j'essai donc d'implémenter les différents exemples qui sont présentés.

    Le problème c'est que voilà, le code suivant (que je vais c/c) ne compilais pas:

    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
    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
    #include <iostream>
     
    class Vector {
        typedef double* iterator;
        typedef const double* const_iterator;
     
        public:
            Vector(double x, double y, double z) {
                element[0] = x;
                element[1] = y;
                element[2] = z;
            }
     
            iterator begin() { return element; }
            iterator end() { return element + 3; }
     
            double getX() { return element[0]; }
            double getY() { return element[1]; }
            double getZ() { return element[2]; }
     
            void setX(double x) { element[0] = x; }
            void setY(double y) { element[1] = y; }
            void setZ(double z) { element[2] = z; }
     
        private:
            double element[3];
    };
     
    enum Ops {
        Add,
        Minus,
        Const
    };
     
    template <typename T, int Op> struct UnaryOp {};
     
    template <typename T> struct UnaryOpBase {
        UnaryOpBase(T t) : It(t) {};
        inline void operator ++() { ++It; }
     
        T It;
    };
     
    template <typename T> struct UnaryOp<T, Minus> : public UnaryOpBase<T> {
        UnaryOp(T t) : UnaryOpBase<T>(t) {};
        inline double operator *() { return -(*It); }
    };
     
    template <typename T> struct UnaryOp<T, Const> : public UnaryOpBase<T> {
        UnaryOp(T t) : UnaryOpBase<T>(t) {};
        inline void operator ++() {};
        inline double operator *() { return It; }
    };
     
    //! ---
     
    template <typename T, typename U, int Op> struct BinaryOp {};
     
    template <typename T, typename U> struct BinaryOpBase {
        BinaryOpBase(T t, U u) : It1(t), It2(u) {};
        inline void operator ++() { ++It1; ++It2; }
     
        T It1;
        U It2;
    };
     
    template <typename T, typename U> struct BinaryOp<T, U, Add> : public BinaryOpBase<T, U> {
        BinaryOp(T t, U u) : BinaryOpBase<T, U>(t, u) {};
        inline double operator *() { *It1 + *It2; }
    };
     
    int main() {
        Vector v(1, 1, 1);
        return 0;
    }
    Le problème est le suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Metaprogrammation-01\main.cpp||In member function `double UnaryOp<T, 1>::operator*()':|
    Metaprogrammation-01\main.cpp|46|error: `It' was not declared in this scope|
    Metaprogrammation-01\main.cpp||In member function `double UnaryOp<T, 2>::operator*()':|
    Metaprogrammation-01\main.cpp|52|error: `It' was not declared in this scope|
    Metaprogrammation-01\main.cpp||In member function `double BinaryOp<T, U, 0>::operator*()':|
    Metaprogrammation-01\main.cpp|69|error: `It1' was not declared in this scope|
    Metaprogrammation-01\main.cpp|69|error: `It2' was not declared in this scope|
    Metaprogrammation-01\main.cpp|69|warning: no return statement in function returning non-void|
    ||=== Build finished: 4 errors, 1 warnings ===|
    Pour régler ce problème, à la ligne46 par exemples, j'ai du mettre:
    inline double operator *() { return -(*UnaryOpBase<T>::It); }
    au lieu de:
    inline double operator *() { return -(*It); }

    Or, dans l'exemple donné dans l'article ce n'est pas le cas. Quelqu'un peut-il m'expliquer pourquoi j'ai du expliciter la provenance de It ? Et sinon, quelqu'un a t-il une autre façon de procéder ?

    Merci d'avance pour vos réponses, et bonnée soirée.

  2. #2
    Membre averti Avatar de zabibof
    Inscrit en
    Février 2007
    Messages
    188
    Détails du profil
    Informations forums :
    Inscription : Février 2007
    Messages : 188
    Points : 344
    Points
    344
    Par défaut
    Il faut utiliser this:
    this->It
    this->It1
    this->It2

  3. #3
    Futur Membre du Club
    Inscrit en
    Juin 2008
    Messages
    9
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 9
    Points : 8
    Points
    8
    Par défaut
    Ah oui exact, je te remercie pour ta réponse!
    Sujet résolu ^^

  4. #4
    yan
    yan est déconnecté
    Rédacteur
    Avatar de yan
    Homme Profil pro
    Ingénieur expert
    Inscrit en
    Mars 2004
    Messages
    10 033
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Ingénieur expert
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Mars 2004
    Messages : 10 033
    Points : 13 968
    Points
    13 968

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

Discussions similaires

  1. [CSS] probléme avec a:hover sur IE (mais bon sous FF)
    Par lafouin dans le forum Mise en page CSS
    Réponses: 6
    Dernier message: 12/02/2009, 15h55
  2. Problème avec les indexes sur une base de données.
    Par osoudee dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 09/02/2006, 09h24
  3. Problème avec ma connexion sur hotmail
    Par shukin dans le forum Autres Logiciels
    Réponses: 6
    Dernier message: 30/01/2006, 14h09
  4. Problème avec port Série sur Win XP
    Par alexorel dans le forum MFC
    Réponses: 9
    Dernier message: 27/10/2005, 15h32

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