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 :

Association entre texte et different type de variable


Sujet :

C++

  1. #1
    Membre habitué
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    Points : 145
    Points
    145
    Par défaut Association entre texte et different type de variable
    Bonjour,

    j'ai un petit problème de conception... Je suis entrain de lire/ecrire un fichier XML et j'aimerais faciliter mon travail en créant une structure pouvant associer un tag (string) avec un pointeur sur une variable!

    Je me suis dit que ne connaissant pas le type de variable je pouvais utiliser
    les templates, mais visiblement ça ne marche pas bien... j'ai l'erreur de compilation suivante:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    C:/wxWidgets-2.8.8/include/wx/window.h:(.text+0x0): multiple definition of `VarLinkedToXmlTag<long>::tmpString2Val(wxString const&)'
    main.o:C:/wxWidgets-2.8.8/include/wx/app.h:(.text+0x0): first defined here
    gui\ConfigXMLS400.o: In function `ZN17VarLinkedToXmlTagIdE13tmpString2ValERK8wxString':
    D:/projects/vicia/Debug/../gui/VarLinkedToXmlTag.hpp:82: multiple definition of `VarLinkedToXmlTag<double>::tmpString2Val(wxString const&)'
    main.o:D:/projects/vicia/Debug/../gui/VarLinkedToXmlTag.hpp:82: first defined here
    gui\ConfigXMLS400.o: In function `ZN17VarLinkedToXmlTagI8wxStringE13tmpString2ValERKS0_':
    D:/projects/vicia/Debug/../gui/VarLinkedToXmlTag.hpp:86: multiple definition of `VarLinkedToXmlTag<wxString>::tmpString2Val(wxString const&)'
    main.o:D:/projects/vicia/Debug/../gui/VarLinkedToXmlTag.hpp:86: first defined here
    gui\ConfigXMLPanel.o: In function `ZN12wxWindowBase12SetSizeHintsEiiiiii':
    Alors voici ma classe: VarLinkedToXmlTag.hpp
    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
    enum
    {
        XML_CHILD = 1,
        XML_ATTRI,
        XML_VALUE,
    };
     
    template <typename T>
    class VarLinkedToXmlTag {
     
    public:
    	VarLinkedToXmlTag(const wxString& tag_, T *val_,const wxString& defaultVal_ , int type_);
    	void readFromXml(wxXmlNode *node);
    	void writeToXml(wxXmlNode *node);
     
    protected:
    	T *val;
    	const int type;
    	const wxString defaultVal;
    	const wxString tag;
     
    private:
    	void tmpString2Val(const wxString &tmp);
    };
     
     
    template <typename T>
    VarLinkedToXmlTag<T>::VarLinkedToXmlTag(const wxString& tag_, T *val_,const wxString& defaultVal_ , int type_)
    	:tag(tag_),val(val_),defaultVal(defaultVal_),type(type_)
    {
     
    }
     
     
     
    template <typename T>
    void VarLinkedToXmlTag<T>::readFromXml(wxXmlNode *node) {
    	wxString tmp;
    	switch(type) {
    	case XML_CHILD:
    		return;
    		break;
    	case XML_ATTRI:
    		tmp = node->GetPropVal(tag,defaultVal);
    		break;
    	case XML_VALUE:
    		if(node->GetName() == tag) {
    			tmp = node->GetNodeContent();
    		}
    		break;
    	}
    	tmpString2Val(tmp);
    }
     
    template <typename T>
    void VarLinkedToXmlTag<T>::tmpString2Val(const wxString &tmp) {
    	int val_adrr=(int)val;
    	wxLogMessage(wxT("the linked value (0x%x) for %s tag is unknown"),val_adrr,tag);
    }
     
    //Specialisation for int value
    void VarLinkedToXmlTag<long>::tmpString2Val(const wxString &tmp) {
    	tmp.ToLong(val);
    }
     
    void VarLinkedToXmlTag<double>::tmpString2Val(const wxString &tmp) {
    	tmp.ToDouble(val);
    }
     
    void VarLinkedToXmlTag<wxString>::tmpString2Val(const wxString &tmp) {
    	*val = tmp;
    }

    Et voici l'appel a la function
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    #include "VarLinkedToXmlTag.hpp"
     
    ConfigXMLCam::ConfigXMLCam(wxXmlNode *node) {
          long thr_alpha;
          VarLinkedToXmlTag<long> *var1 = new VarLinkedToXmlTag<long>(_T("thr_alpha"),&thr_alpha,_T("10"),XML_ATTRI);
          var1->readFromXml(node);
    }
    bon j'utilise les wxWidget donc il faut pas faire attention aux macros _T() et au wxString ... ca reviens a utiliser qqc de similaire a std::string

  2. #2
    Expert confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    3 549
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

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

    Informations forums :
    Inscription : Décembre 2003
    Messages : 3 549
    Points : 4 625
    Points
    4 625
    Par défaut
    Tu instancies tmpString2Val avant de le spécialiser.

  3. #3
    Membre habitué
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    Points : 145
    Points
    145
    Par défaut
    Je suis pas sur de comprendre ce que tu me répond... mais le problème c'est surtout que je ne sais pas comment corriger çà !!!

    peux tu approfondir ???

  4. #4
    Expert confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    3 549
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

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

    Informations forums :
    Inscription : Décembre 2003
    Messages : 3 549
    Points : 4 625
    Points
    4 625
    Par défaut
    Facile, spécialise avant d'instancier.
    Ainsi tu résous le problème.

  5. #5
    Membre habitué
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    Points : 145
    Points
    145
    Par défaut
    Specialiser la class ??
    Je doit donc redéfinir pour chaque type toute la classe entiere ???
    C'est vraiment pas pratique il y a pas une solution plus simple pour specialiser la fonction sans spécialiser la class

  6. #6
    Membre habitué
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    Points : 145
    Points
    145
    Par défaut
    Je viens de tester ca :

    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
     
    template <typename T>
    void tmpString2Val(const wxString &tmp, T & val) {
    	int val_adrr=(int)&val;
    	wxLogMessage(wxT("the linked value (0x%x) is unknown"),val_adrr);
    }
     
    template <>
    void tmpString2Val<long>(const wxString &tmp, long & val) {
    	tmp.ToLong(&val);
    }
     
    template <>
    void tmpString2Val<double>(const wxString &tmp, double & val) {
    	tmp.ToDouble(&val);
    }
     
    template <>
    void tmpString2Val<wxString>(const wxString &tmp, wxString & val) {
    	val = tmp;
    }
    C'est pourtant très similaire a ce qu'il y a dans http://cpp.developpez.com/faq/cpp/?p...specialisation, mais j'ai toujours le meme genre de problème

  7. #7
    Rédacteur
    Avatar de 3DArchi
    Profil pro
    Inscrit en
    Juin 2008
    Messages
    7 634
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2008
    Messages : 7 634
    Points : 13 017
    Points
    13 017
    Par défaut
    Chez moi, le code suivant compile avec Visual et gcc:
    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
    76
    77
    78
    79
    80
    81
     
    #include <iostream>
    #include <string>
     
    typedef std::string wxString;
     
    enum
    {
        XML_CHILD = 1,
        XML_ATTRI,
        XML_VALUE,
    };
     
    template <typename T>
    class VarLinkedToXmlTag {
     
    public:
       VarLinkedToXmlTag(const wxString& tag_, T *val_,const wxString& defaultVal_ , int type_);
     
       void VarLinkedToXmlTag<T>::readFromXml();
     
    protected:
    	const wxString tag;
    	T *val;
    	const wxString defaultVal;
    	const int type;
     
    private:
    	void tmpString2Val(const wxString &tmp, T&);
    };
     
     
    template <typename T>
    VarLinkedToXmlTag<T>::VarLinkedToXmlTag(const wxString& tag_, T *val_,const wxString& defaultVal_ , int type_)
    	:tag(tag_),val(val_),defaultVal(defaultVal_),type(type_)
    {
     
    }
     
     
     
    template <typename T>
    void VarLinkedToXmlTag<T>::readFromXml() {
    	wxString tmp;
    	switch(type) {
    	case XML_CHILD:
    		return;
    		break;
    	case XML_ATTRI:
    		tmp = "attri";
    		break;
    	case XML_VALUE:
    		tmp = "value";
    		break;
    	}
    	tmpString2Val(tmp);
    }
     
    template <typename T>
    void tmpString2Val(const wxString &tmp, T & val) {
    }
     
    template <>
    void tmpString2Val<long>(const wxString &tmp, long & val) {
    }
     
    template <>
    void tmpString2Val<double>(const wxString &tmp, double & val) {
    }
     
    template <>
    void tmpString2Val<wxString>(const wxString &tmp, wxString & val) {
    	val = tmp;
    }
     
    int main()
    {
       long L_l;
       VarLinkedToXmlTag<long> L_1("toto",&L_l,"2",XML_ATTRI);
       return 0;
    }

  8. #8
    Membre habitué
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    Points : 145
    Points
    145
    Par défaut
    Ton code marche tres bien chez moi aussi, mais quand je l'integre dans mon projet c'est la catastrophe et je n'arrive pas a comprendre pk ??

    Enfait je viens de trouver la faille, je creer 2 objects dans deux *.o different, plus le main.o...

    main.cpp inclus objectA.h
    objectA.h inclus objectB.h
    objectB.h inclus la template VarLinkedToXmlTag.hpp


    la template va donc etre definis dans main.o (objectA.h -> objectB.h -> template)
    elle va etre redefinis dans objectA.o (objectB.h -> template)
    puis va etre refefinis dans objectB.o ...

    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
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\ObjectB.o ..\src\ObjectB.cpp
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\ObjectA.o ..\src\ObjectA.cpp
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -osrc\main.o ..\src\main.cpp
    g++ -otemplate.exe src\main.o src\ObjectB.o src\ObjectA.o
    src\ObjectB.o: In function `Z13tmpString2ValIlEvRKSsRT_':
    D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:72: multiple definition of `void tmpString2Val<long>(std::string const&, long&)'
    src\main.o:D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:72: first defined here
    src\ObjectB.o: In function `Z13tmpString2ValIdEvRKSsRT_':
    D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:77: multiple definition of `void tmpString2Val<double>(std::string const&, double&)'
    src\main.o:D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:77: first defined here
    src\ObjectB.o: In function `Z13tmpString2ValISsEvRKSsRT_':
    D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:82: multiple definition of `void tmpString2Val<std::string>(std::string const&, std::string&)'
    src\main.o:D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:82: first defined here
    src\ObjectA.o: In function `Z13tmpString2ValIlEvRKSsRT_':
    D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:72: multiple definition of `void tmpString2Val<long>(std::string const&, long&)'
    src\main.o:D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:72: first defined here
    src\ObjectA.o: In function `Z13tmpString2ValIdEvRKSsRT_':
    D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:77: multiple definition of `void tmpString2Val<double>(std::string const&, double&)'
    src\main.o:D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:77: first defined here
    src\ObjectA.o: In function `Z13tmpString2ValISsEvRKSsRT_':
    D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:82: multiple definition of `void tmpString2Val<std::string>(std::string const&, std::string&)'
    src\main.o:D:/projects/template/Debug/../src/VarLinkedToXmlTag.hpp:82: first defined here
    collect2: ld returned 1 exit status
    Build error occurred, build is stopped
    Time consumed: 1594  ms.
    Donc cela donne aux moments du link plein de probleme, car il y a plusieurs fois la définition des class...
    Peut être cela vient du fait que je compile avec MinGW!!!, mais ca me parait bizarre que l'on ne peux pas utiliser la spécialisation avec ce compilateur.

    Pourtant d'apres think in C++:
    Tout ce qui est précédé par template<...>signifie que le compilateur n'allouera pas d'espace de stockage pour cela à ce moment, mais, à la place, attendra qu'il lui soit dit de le faire (par l'instanciation du template). En outre, quelque part dans le compilateur ou le linker il y a un mécanisme pour supprimer les définitions multiples d'un template identique. Donc vous placerez presque toujours la déclaration etla définition complètes dans le fichier d'en-tête, par commodité

    Si jamais vous voulez tester mon code de sample en utilisant les fichiers suivants:
    http://elraton.free.fr/work/s400/templates/src/
    ou en archive rar (2ko)
    http://elraton.free.fr/work/s400/templates/src.rar

  9. #9
    Membre habitué
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 40

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    Points : 145
    Points
    145
    Par défaut
    Voila la solution,

    DOIT ETRE AJOUTE DANS LA FAQ


    Si la function est spécialisé ce n'est plus une template donc est inclus directement dans le code. Si cette dernière se trouve dans un .hpp elle sera donc définis a la compilation de chaque object!

    Il faut donc soit la mettre dans un .cpp, soit la définir en include:

    Pour plus d'infos lire ca: http://www.developpez.net/forums/d53...tion-multiple/

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

Discussions similaires

  1. Réponses: 9
    Dernier message: 26/05/2007, 14h53
  2. Probleme de type de variable entrée
    Par aladin_lampemagic dans le forum C++
    Réponses: 6
    Dernier message: 13/03/2007, 01h40
  3. difference entre deux dates de type Timestamp
    Par err dans le forum Requêtes
    Réponses: 4
    Dernier message: 10/08/2006, 18h44
  4. difference entre TEXT et VARCHAR ?
    Par goony dans le forum MS SQL Server
    Réponses: 1
    Dernier message: 12/08/2005, 07h39
  5. Types de variables entre mysql/php et flash
    Par ramses83 dans le forum Flash
    Réponses: 2
    Dernier message: 06/10/2003, 18h35

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