#include"iostream"
using namespace std;
class xxx
{
public:
char **a;
xxx();
~xxx();
};
xxx :: xxx()
{
a=new char*;
*a="10";
cout<<endl<<*a; // works fine
cout<<"Enter name:";
cin>>*a; // segmentation fault??
}
xxx :: ~xxx()
{
delete a;
}
main()
{
xxx x;
cout<<*x.a;
}
爲什麼我不能使用cin更改名稱字段? 當我調用構造函數時,它將值賦給變量,但編輯時顯示以下錯誤: 程序已停止工作。 方法也會出現同樣的問題。我錯過了什麼。在你的代碼爲什麼此代碼顯示分段錯誤?
'main'回報'int'。 –
使用std :: string。 – 2017-06-22 13:22:59
你應該閱讀[很好的C++書](https:// stackoverflow。com/questions/388242/the-definitive-c-book-guide-and-list) – nefas