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++

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    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
    Par défaut
    Tu instancies tmpString2Val avant de le spécialiser.

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

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    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
    Par défaut
    Facile, spécialise avant d'instancier.
    Ainsi tu résous le problème.

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

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    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 confirmé
    Inscrit en
    Juin 2003
    Messages
    223
    Détails du profil
    Informations personnelles :
    Âge : 41

    Informations forums :
    Inscription : Juin 2003
    Messages : 223
    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

+ 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