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

Langage C++ Discussion :

constructeur pour MyClass inst=valeur


Sujet :

Langage C++

  1. #1
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut constructeur pour MyClass inst=valeur
    bonjour,

    je connais le constructeur:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class myclass{
    myclasse(type objet);
    ...
    };
     
    myclass::myclass(type objet) {
    ...
    }
     
    int main(){
       myclass objet1(objet2);
    }
    Comment déclarer un constructeur qui permette ceci?:
    une idée?

  2. #2
    Expert confirmé
    Homme Profil pro
    Ingénieur développement matériel électronique
    Inscrit en
    Décembre 2015
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Ingénieur développement matériel électronique
    Secteur : High Tech - Électronique et micro-électronique

    Informations forums :
    Inscription : Décembre 2015
    Messages : 1 599
    Par défaut
    Bonjour,

    Pour la construction avec =, elle indique que l'on ne veut pas une construction directe mais une construction par copie (à ne pas confondre avec une assignation par copie). Le compilateur produit comme si on avait écrit myclass objet1 = myclass(objet2);, on crée un temporaire construit à partir de objet2 et on créé objet1 à partir de ce temporaire.

    Ça doit donc directement marcher, à moins que tu n'aies ajouté d'autres contraintes (constructeur par copie inaccessible, accès explicit au constructeur, ambiguïté, ...)

    Et la solution est dans l'ajustement, la correction ou le contournement de ces contraintes, nécessite plus d'infos dont le texte exact de l'erreur indiquée.

  3. #3
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    #include <string>
    class myclass{
    public:
      myclass(std::string valeur);
    };
     
    int main(){
      std::string valeur="abc";
      myclass objet=valeur;
      myclass objet2="456";
      myclass objet3("esdfg");
    }

    "myclass objet= valeur" fonctionne, "myclass objet3("esdfg") aussi, mais pas "myclass objet2="456""

    est-il possible de construire, avec '=', à partir d'une chaine?


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    main.cpp: In function ‘int main()’:
    main.cpp:10:18: error: conversion from ‘const char [4]’ to non-scalar type ‘myclass’ requested
       10 |   myclass objet2="456";
          |                  ^~~~~

  4. #4
    Modérateur

    Avatar de Bktero
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2009
    Messages
    4 493
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire Atlantique (Pays de la Loire)

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

    Informations forums :
    Inscription : Juin 2009
    Messages : 4 493
    Billets dans le blog
    1
    Par défaut
    Un const char* n'est pas une std::string.

    Tu n'as pas de constructeur qui accepte un const char* donc le compilateur n'est pas content.

    Il te faut un myclass(const char* valeur);

  5. #5
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut
    merci pour vos réponses.

    autre problème: ambigous
    voici mes constructeurs;
    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
    nombre::nombre() { }
     
    nombre::nombre(const char * valeur_init){
      std::string copie(valeur_init);
      if(copie.at(0)=='-'){
        positif=false;
        copie=copie.substr(1);
      }
      else if(copie.at(0)=='+'){
        positif=true;
        copie=copie.substr(1);
      }
      else
        positif=true;
      int indice=copie.find(',');
      if(indice!=std::string::npos){
        partie_entiere=copie.substr(0,indice);
        partie_decimale=copie.substr(indice+1);
      }
      else{
        partie_entiere=copie;
        partie_decimale="0";
      }
      *this=this->normalise();
    }
     
    nombre::nombre(nombre const &nb){
      positif=nb.positif;
      partie_entiere=nb.partie_entiere;
      partie_decimale=nb.partie_decimale;
    }
     
    nombre::nombre(std::string const &valeur_init){
      std::string copie(valeur_init);
      if(copie.at(0)=='-'){
        positif=false;
        copie=copie.substr(1);
      }
      else if(copie.at(0)=='+'){
        positif=true;
        copie=copie.substr(1);
      }
      else
        positif=true;
      int indice=copie.find(',');
      if(indice!=std::string::npos){
        partie_entiere=copie.substr(0,indice);
        partie_decimale=copie.substr(indice+1);
      }
      else{
        partie_entiere=copie;
        partie_decimale="0";
      }
      *this=this->normalise();
    }
    ma fonction main():
    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
    #include <string>
    #include <iostream>
     
    #include "nombre.hpp"
    #include "operateurs.hpp"
     
    int main(){
      std::string a,b,c;
      std::cin>>a>>b>>c;
      nombre n1,n2,n3("123");
      n1=a;
      n2=b;
      n3=c;
      nombre n4("215565845");
      std::cout<<n4<<std::endl;
      std::cout<<n1<<" + "<<n2<<" = "<<n1+n2<<std::endl;
      std::cout<<n1<<" - "<<n2<<" = "<<n1-n2<<std::endl;
      std::cout<<n1<<" * "<<n2<<" = "<<n1*n2<<std::endl;
      std::cout<<n1<<" ^ "<<n2<<" = "<<n1.exposant(n2)<<std::endl;
      n1+=n2;
      std::cout<<"+ "<<n2<<" =  "<<n1<<std::endl;
      n1-=n2;
      std::cout<<"- "<<n2<<" = "<<n1<<std::endl;
      n1*=n2;
      std::cout<<"* "<<n2<<" = "<<n1<<std::endl;
      n4=n3=n2=n1;
      std::cout<<n1<<" "<<n2<<" "<<n3<<" "<<n4<<std::endl;
    }
    et voici les message d'erreur:
    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
    $ g++ -g -o test *cpp
    nombre.cpp: In member function ‘nombre nombre::operator*(const nombre&) const’:
    nombre.cpp:185:12: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      185 |   resultat="0";
          |            ^~~
    In file included from nombre.cpp:4:
    nombre.hpp:28:11: note: candidate: ‘nombre& nombre::operator=(const std::string&)28 |   nombre& operator=(std::string const &nombre_str);
          |           ^~~~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp:190:10: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      190 |       un="1";
          |          ^~~
    nombre.hpp:28:11: note: candidate: ‘nombre& nombre::operator=(const std::string&)28 |   nombre& operator=(std::string const &nombre_str);
          |           ^~~~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp: In member function ‘nombre nombre::exposant(const nombre&) const’:
    nombre.cpp:371:14: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      371 |     resultat="0";
          |              ^~~
    nombre.cpp:221:9: note: candidate: ‘nombre& nombre::operator=(const std::string&)221 | nombre& nombre::operator=(std::string const &nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp:374:14: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      374 |     resultat="1";
          |              ^~~
    nombre.cpp:221:9: note: candidate: ‘nombre& nombre::operator=(const std::string&)221 | nombre& nombre::operator=(std::string const &nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp:380:12: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      380 |         un="1";
          |            ^~~
    nombre.cpp:221:9: note: candidate: ‘nombre& nombre::operator=(const std::string&)221 | nombre& nombre::operator=(std::string const &nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    avez-vous une idée?

  6. #6
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut
    je comprends: il faut une surcharge de operator=(const char* valeur_init)
    je vais essayer cela et je reviens

  7. #7
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut
    non ça ne fonctionne pas encore
    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
    nombre& nombre::operator=(const char *nombre_str){
      std::string copie(nombre_str);
      if(copie.at(0)=='-'){
        positif=false;
        copie=copie.substr(1);
      }
      else if(copie.at(0)=='+'){
        positif=true;
        copie=copie.substr(1);
      }
      else
        positif=true;
      int indice=copie.find(',');
      if(indice!=std::string::npos){
        partie_entiere=copie.substr(0,indice);
        partie_decimale=copie.substr(indice+1);
      }
      else{
        partie_entiere=copie;
        partie_decimale="0";
      }
      *this=this->normalise();
      return *this;
    }
     
    nombre& nombre::operator=(std::string const &nombre_str){
      std::string copie(nombre_str);
      if(copie.at(0)=='-'){
        positif=false;
        copie=copie.substr(1);
      }
      else if(copie.at(0)=='+'){
        positif=true;
        copie=copie.substr(1);
      }
      else
        positif=true;
      int indice=copie.find(',');
      if(indice!=std::string::npos){
        partie_entiere=copie.substr(0,indice);
        partie_decimale=copie.substr(indice+1);
      }
      else{
        partie_entiere=copie;
        partie_decimale="0";
      }
      *this=this->normalise();
      return *this;
    }
    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
    nombre::nombre() { }
     
    nombre::nombre(const char * valeur_init){
      std::string copie(valeur_init);
      if(copie.at(0)=='-'){
        positif=false;
        copie=copie.substr(1);
      }
      else if(copie.at(0)=='+'){
        positif=true;
        copie=copie.substr(1);
      }
      else
        positif=true;
      int indice=copie.find(',');
      if(indice!=std::string::npos){
        partie_entiere=copie.substr(0,indice);
        partie_decimale=copie.substr(indice+1);
      }
      else{
        partie_entiere=copie;
        partie_decimale="0";
      }
      *this=this->normalise();
    }
     
    nombre::nombre(nombre const &nb){
      positif=nb.positif;
      partie_entiere=nb.partie_entiere;
      partie_decimale=nb.partie_decimale;
    }
    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
    $ g++ -g -o test *cpp
    nombre.cpp: In member function ‘nombre nombre::operator*(const nombre&) const’:
    nombre.cpp:185:12: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      185 |   resultat="0";
          |            ^~~
    In file included from nombre.cpp:4:
    nombre.hpp:28:11: note: candidate: ‘nombre& nombre::operator=(const std::string&)28 |   nombre& operator=(std::string const &nombre_str);
          |           ^~~~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp:190:10: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      190 |       un="1";
          |          ^~~
    nombre.hpp:28:11: note: candidate: ‘nombre& nombre::operator=(const std::string&)28 |   nombre& operator=(std::string const &nombre_str);
          |           ^~~~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp: At global scope:
    nombre.cpp:221:9: error: no declaration matches ‘nombre& nombre::operator=(const char*)221 | nombre& nombre::operator=(const char *nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidates are: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.hpp:28:11: note:                 ‘nombre& nombre::operator=(const std::string&)28 |   nombre& operator=(std::string const &nombre_str);
          |           ^~~~~~~~
    nombre.hpp:4:7: note: ‘class nombre’ defined here
        4 | class nombre{
          |       ^~~~~~
    nombre.cpp: In member function ‘nombre nombre::exposant(const nombre&) const’:
    nombre.cpp:397:14: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      397 |     resultat="0";
          |              ^~~
    nombre.cpp:246:9: note: candidate: ‘nombre& nombre::operator=(const std::string&)246 | nombre& nombre::operator=(std::string const &nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp:400:14: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      400 |     resultat="1";
          |              ^~~
    nombre.cpp:246:9: note: candidate: ‘nombre& nombre::operator=(const std::string&)246 | nombre& nombre::operator=(std::string const &nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    nombre.cpp:406:12: error: ambiguous overload foroperator=’ (operand types are ‘nombre’ andconst char [2])
      406 |         un="1";
          |            ^~~
    nombre.cpp:246:9: note: candidate: ‘nombre& nombre::operator=(const std::string&)246 | nombre& nombre::operator=(std::string const &nombre_str){
          |         ^~~~~~
    nombre.hpp:4:7: note: candidate: ‘nombre& nombre::operator=(const nombre&)4 | class nombre{
          |       ^~~~~~
    une idée?

  8. #8
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut
    et si je faisais:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    std::string nouveau="123"
    nombre n(nouveau)

  9. #9
    Membre éclairé

    Homme Profil pro
    développeur à la maison
    Inscrit en
    Septembre 2006
    Messages
    402
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Tarn et Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : développeur à la maison

    Informations forums :
    Inscription : Septembre 2006
    Messages : 402
    Billets dans le blog
    16
    Par défaut
    ça y est ça fonctionne
    j'avais oublié ce prototype dans la classe:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    nombre& operator=(const char * nombre_str);

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

Discussions similaires

  1. Réponses: 5
    Dernier message: 09/09/2005, 17h51
  2. problème pour récupérer une valeur dans ma bd (débutante)
    Par auryn111 dans le forum Langage SQL
    Réponses: 1
    Dernier message: 26/08/2005, 17h49
  3. [Conception][constructeur] pour faire un tableau
    Par vasilov dans le forum Collection et Stream
    Réponses: 6
    Dernier message: 20/07/2005, 10h58
  4. API pour récupérer la valeur d'un champ Edit
    Par buteiduil dans le forum Windows
    Réponses: 4
    Dernier message: 15/05/2005, 22h32
  5. Réponses: 6
    Dernier message: 28/10/2004, 10h05

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