-5

I am new in programming in Cpp and I am doing a program with a class that must contains one name, the sex, the age and the phone number of the person.

I got three errors.

There is my program:

enter code here

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class infoPersonne
{
   char *nom [40];
   char *sexe;
   int age;
   char *notel [12];
   public:
      infoPersonne(char * , char *, int , char *);
      infoPersonne();
      infoPersonne(infoPersonne &ip);
      ~infoPersonne();

      void affiche();

};


infoPersonne::infoPersonne(char *n, char *s, int a, char *nt) // contructeur
{
    std::string nom=n;
    std::string sexe=s;
    age=a;
    std::string notel=nt;
    cout<< "construction de l'information d'une personne"<<endl;
}

infoPersonne::infoPersonne( infoPersonne &ip) // constructeur par recopie
{
    std::string nom  = ip.nom;  
    std::string sexe = ip.sexe;
    age=ip.age;
    std::string notel=ip.nt;
 }

 infoPersonne::infoPersonne() // constructeur par défaut
 {
     std::string nom  = "";
     std::string sexe = "";
     age = 0;
     std::string notel = "0";
 }

 infoPersonne:: ~infoPersonne() // destructeur
 {
     cout<<"destruction de l'information d'une personne"<<endl;
     delete []nom;
     delete sexe;
     delete []notel;
 }

 void infoPersonne:: affiche()
 {
     cout << endl << "Nom  :" << nom
          << endl << "Sexe :" << sexe
          << endl << "Age  :" << age
          << endl << "Telephone : " << notel << endl;
 }


using namespace std;

int main(int argc, char *argv[])
{
    infoPersonne A();
    infoPersonne B("Louise Messier","F",54, "514-756-8890");
    A.affiche();
    B.affiche();

    system("PAUSE");
    return EXIT_SUCCESS;
 }

My first error appear on the line of the constructor by copy infoPersonne::infoPersonne(infoPersonne &ip) at the specific line std::string nom=ip.nom. ERROR IS : conversion from 'char* [40]' to non-scalar type 'std::string {aka std::basic_string}' requested| And I have another error: 'class infoPersonne' has no member named 'nt' but the member nt is defined? And the third one is : request for member 'affiche' in 'A', which is of non-class type 'infoPersonne()'|

Can you help me please ? Thank you in advance.

Missmile03
  • 21
  • 1
  • 1
  • 1
    What is `char *nom[40];` and why is it declared as `char *[40]`? What did you try to achieve by declaring an array of 40 pointers? – AnT stands with Russia Dec 26 '17 at 20:06
  • 2
    `char *nom [40]` is an array of 40 char pointers. You probably meant `char nom [40]`, an array of 40 chars. – rustyx Dec 26 '17 at 20:07
  • 1
    You already know std::string. Use it for the name. –  Dec 26 '17 at 20:07
  • `char *sexe;` most likely should be `char sexe;`. –  Dec 26 '17 at 20:09
  • 2
    @Missmile03 The code does not make sense. – Vlad from Moscow Dec 26 '17 at 20:09
  • `std::string nom = ip.nom;` in the constructor most likely should be `nom = ip.nom;`. That would work if you use std::string for the name. –  Dec 26 '17 at 20:10
  • @manni66 Close, more likely `nom = ip.nom`. And the same in the other constructors where the OP defines *local* variables shadowing the member variables. Not that the assignment would work anyway. – Some programmer dude Dec 26 '17 at 20:12
  • 5
    I suggest you take a few steps back, [get a couple of good beginners book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start over. – Some programmer dude Dec 26 '17 at 20:12
  • If you're using `std::string`, what is the reason for using `char *` at all? – PaulMcKenzie Dec 26 '17 at 20:17
  • I was using array of char because I want to have enough space to contain the name of the person. But what is the problem with my first error "conversion from char * [40]..." Is it because I used an array ? So if I understand your answer, I must only used char nom [40] everywhere ? – Missmile03 Dec 26 '17 at 20:46
  • And why I got the error on the member named nt? Why it is saying that there is no member on this name ? – Missmile03 Dec 26 '17 at 20:51
  • _I was using array of char because I want to have enough space to contain the name of the person_ an array has a fixed size, therefore there is no guarantee that there is enough space. A std::string allocates the required space, so it has always enough. –  Dec 26 '17 at 21:34
  • _Why it is saying that there is no member on this name ?_ because there is none. –  Dec 26 '17 at 21:36
  • When declaring functions or methods, supply the variable names to reduce the ambiguity of the parameters, especially when two of the same type or more are in a row. – Thomas Matthews Dec 26 '17 at 21:49
  • Remember to pass `std::string` by reference if you modify it, or `const` reference if the string is not modified. – Thomas Matthews Dec 26 '17 at 21:50
  • In your constructors, you are declaring `std::string` variables which will disappear after execution leaves the function; so you may want to delete them (as they have no functionality). Or you may want to use `strcpy` to copy the contents of the parameters to the member character arrays. – Thomas Matthews Dec 26 '17 at 21:52
  • Ok, I tried to do by step so I tried to do not define the constructor of copy and everything was fine. But all my characters variable wasn't correctly displayed on the screen. There is what the result of my program: Nom : 0x6afe18, Sexe: i lyQilm"usSwa, Age: 54 and Telephone: 0x6afec0. – Missmile03 26 mins ago. And to anwser Manni66 about the member nt. On my code I define it std::string notel = nt in the constructor like the name and sex. Can you help me again please ? – Missmile03 Dec 26 '17 at 21:54
  • `std::string notel = nt ` defines a local variable notel, nothing else. Follow Someprogrammerdude‘s advice: get some beginners books. –  Dec 26 '17 at 23:01
  • I don't have time to buy some books. I have to give my homework the 31th of December. So I must find my error before that date. Ok, if I follow your advice, I must redefine my variable by string name [40], string sexe... and I have to change my constructors with the parameter the same and not used the pointer like I do on infoPersonne constructor (char *n, char *s, int 2, char *nt). Can you show me example of that please ? – Missmile03 Dec 26 '17 at 23:49

1 Answers1

-1

std::strings are much easier to work with than char arrays for beginners. I'd suggest you use them unless there is a reason not to.

There are many issues with the code. See if you can get less code working first. How about one function that you call from main, passing in stings and char array pointers so you can get that skill down.

CMdude1
  • 9
  • 1
  • I tried to do not define the constructor of copy and everything was fine. But all my characters variable wasn't correctly displayed on the screen. – Missmile03 Dec 26 '17 at 21:15
  • Look what I obtain when I don't define the constructor of copy: – Missmile03 Dec 26 '17 at 21:18
  • I am sorry I tried to copy/paste on the screen but it is not work: There is what the result of my program: Nom : 0x6afe18, Sexe: i lyQilm"usSwa, Age: 54 and Telephone: 0x6afec0. – Missmile03 Dec 26 '17 at 21:21