0

I am just trying to design a class to set and get the details of a student in C++. So,to set the details of the student,i called set_data(name,roll) method.But this compilation error is coming.I have passed the same data type as actual arguments to the function as expected.I cant understand why error is coming.

G:\>g++ 5.cpp
5.cpp: In function 'int main()'
5.cpp:26:23: warning: deprecate
rite-strings]
 obj.set_data("asdf",15);

The code is ---

#include<iostream>
using namespace std;
#include<string.h>
#include<stdlib.h>      
class pntr_obj
{
char *name;
int roll;
public:
void set_data(char *name,int roll)
{
this->name=name;
this->roll=roll;
}
void print()
{
cout<<"name is "<<this->name<<endl;

cout<<"roll is "<<this->roll<<endl;
}
};
int main()
{
pntr_obj obj;

obj.set_data("asdf",15);
obj.print();
return 0;
}
5
  • 3
    indent your code, please Commented May 2, 2016 at 14:02
  • 1
    have you learned what the type of a string literal ("asdf") is yet? Commented May 2, 2016 at 14:03
  • 1
    Change *char to string. Commented May 2, 2016 at 14:03
  • "asdf" is taken as a string,correct me if i'm wrong Commented May 2, 2016 at 14:06
  • Next time, please search for the error message / warning you get before posting a new question. This was trivial to find. Commented May 2, 2016 at 14:20

1 Answer 1

3

I have passed the same data type as actual arguments to the function as expected.

No. "asdf" is string literal with type const char[5], it might decay to const char*, but not char *.

In C, string literals are of type char[], and can be assigned directly to a (non-const) char*. C++03 allowed it as well (but deprecated it, as literals are const in C++). C++11 no longer allows such assignments without a cast.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.