0

i'm trying the below basic program, where i'm getting a warning on compailing.

#include<iostream>
#include<cstring>

using namespace std;

class employee
{
private:
    char name[20];
    int age;
    float sal;
public:
    void setdata (char *n, int a, float s)
    {
        strcpy (name, n);
        age = a;
        sal = s;
    }
    void showdata()
    {
        cout << endl << name << '\t' << age << '\t' << sal << endl;
    }
};


int main ()
{

    employee e1;

    e1.setdata("ajay", 23, 4500.50);
    e1.showdata();
    return 0;
}

But, as soon as i compile this i get the below error;

deprecated conversion from string constant to 'char*' [-Wwrite-strings]

Please explain why am i getting this error, and also the resolution to eliminate. and how dose the resolution works..

Thanks in advance.

  • Possible duplicate of [C++ warning: deprecated conversion from string constant to ‘char*’](http://stackoverflow.com/q/21529194/1708801) – Shafik Yaghmour Mar 11 '15 at 19:20
  • @dasblinkenlight , only the title is identical,.. doubt and code are totally different, can you help me understand whats happening here and how to correct it..?? – Lazy Shetty Mar 11 '15 at 19:32
  • @LazyShetty The code is always different, but the problem is the same. You are missing `const` in front of `char *n` in `setdata`. – Sergey Kalinichenko Mar 11 '15 at 19:37
  • yes it solved the warning. – Lazy Shetty Mar 11 '15 at 19:43
  • but does this means "if i'm assigning value received as an argument to a constant pointer (in this case "name") then i must mention the const with the argument (like const char *n)"..?? – Lazy Shetty Mar 11 '15 at 19:47

0 Answers0