0

I have been learning c++ since the last three months and i am currently stuck on some twisted warning which my compiler is giving to me. The warning states the following: "ISO C++ forbids converting a const string to char*". I am using Eclipse Helios 2011 32-bit IDE. When i declare both the name and s as string it gives me an error stating: "cannot convert a string to char *". A little insight and a slight hint regarding this problem would be appreciated.

For code it is as follows:

#include<iostream>
#include<cstring>

using namespace std;

class person
{
    char name[20];
    float age;

public:
person(char *s,float a)
{
    strcpy(name,s);
    age=a;
}
person& greater(person &x)
{
    if(x.age>=age)
        return x;
    else
        return *this;
}

void display()
{
    cout<<"Name "<<name<<endl;
    cout<<"Age "<<age<<endl;
}

};

int main()
{

person P1("John",37.40) ,P2("Kramer",40.01),P3("Jason",57.80);
person P=P1.greater(P2);
cout<<"Elder person between P1 and P2 is: "<<endl;
P.display();
person P4=P2.greater(P3);
cout<<"Elder person between P2 and P3 is: "<<endl;
P4.display();



}
  • I cannot see any occurrence of `std::string` in your exposed code. Please, provide the code you have trouble with. – Scheff's Cat Jul 07 '20 at 05:32
  • There's more in the dup that what you have here, but you need to change the `person` constructor to take a `const char *`: `person(const char *s, float a)`. – 1201ProgramAlarm Jul 07 '20 at 05:32
  • You need to learn about const correctness, in your code you are using non-const aka lvalue references where you should use const reference and `char *` instead of `const char *` – Slava Jul 07 '20 at 05:34

0 Answers0