I have a class:
class Person
{
string* name;
Person(string nm)
{
name=&nm;
}
void print()
{
cout<<"That name: "<<*name<<endl;
}
};
Then:
main()
{
Person first("Jack");
first.print();
}
Console shows "Segemntation fault". Ok, but where am I wrong and how should I write this code to print that? PS. I have to use a field "string* name" in this class.
I solved the problem. I had to do it in the class:
class Person
{
string* name;
Person(string* nm)
{
name=nm;
}
void print()
{
cout<<"That name: "<<*name<<endl;
}
};
And then in funcion main:
main()
{
string str_name="Jack";
Person first(&str_name);
first.print();
}
Now, it's all correct.