简体   繁体   中英

errror: invalid conversion from 'const char*' to 'int'

I am trying to add to a class a person's day, month and year of birth. As of now, i am at trying to include the day of birth, had lots of errors, managed to get rid of most of them but i still have this one left(at first, i included at the same time the day, month and year of birth but i had many erros so i decided to try and fix at least one of them). In the code i also have the birth date as a char and that works fine but i need those values to work with them later.

#include <iostream>
#include <cstring>
using namespace std;
class Persoana
{
private:
    char nume[20];
    char data_nastere[20];
    int zi;
public:
    Persoana(char *nume="", char *data_nastere="", int zi="");//this is where i have the error
    void setNume(char *nume);
    char* getNume();
    void setDataNastere(char *data_nastere);
    char* getDataNastere();
    void setZi(int zi);
    int getZi();
    void afisare();
};
Persoana::Persoana(char *nume, char *data_nastere, int zi)
{
    setNume(nume);
    setDataNastere(data_nastere);
    setZi(zi);
}
void Persoana::setNume(char *nume)
{
    strcpy(this->nume, nume);
}
char* Persoana::getNume()
{
    return nume;
}
void Persoana::setDataNastere(char *data_nastere)
{
    strcpy(this->data_nastere, data_nastere);
}
char* Persoana::getDataNastere()
{
    return data_nastere;
}
void Persoana::setZi(int zi)
{
    this->zi=zi;
}
int Persoana::getZi()
{
    return zi;
}
void Persoana::afisare()
{
    cout<<"Nume: "<<nume<<endl;
    cout<<"Data nasterii este: "<<data_nastere<<endl<<endl;
    cout<<zi<<endl;
}
int main()
{
    Persoana p[] = {Persoana("Calin Dorina", "12 02 2000", 12), Persoana("Mihaela Banu", "25 04 2001", 25)};
    p[0].afisare();
    //p[1].afisare();
}

You try to assign a pointer to literal string (it's pointer to const char which is fist letter in this string) to the integer property z in constructor: int zi="" . C++ hasn't default conversion from const char* to int . You should use integer default values for integer properties.

One of you said to declare int zi=0 and thats the right answer. I tried doing it before but where i declared the private variables. Thanks for helping!

In case someone else has this problem, here it is how its supposed to be:

Persoana(char *nume="", char *data_nastere="", int zi=0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM