如果網(wǎng)上各種誤人子弟的方法可以解決問題的話 我就不在這逼逼了
重點:出現(xiàn)這個肯定是你的代碼的問題 VS自己少東西的可能性幾乎為0
而問題基本都出在指針的使用上 一般都是你調(diào)用你的指針指向了錯誤的東西 / 你調(diào)用你的指針釋放了奇怪的內(nèi)容
以下為示范代碼:
#include<iostream>
#include<ctime>
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
class Name {
private:
char* first;
char* second;
char* third;
public:
Name(string f = "", string s = "", string t = "")
{
first = new char[f.length() + 1];
second = new char[s.length() + 1];
third = new char[t.length() + 1];
strcpy_s(first, f.length() + 1, f.c_str());
strcpy_s(second, s.length() + 1, s.c_str());
strcpy_s(third, t.length() + 1, t.c_str());
}
Name(Name& name)
{
}
~Name()
{
cout << "發(fā)病了" << endl;
delete first;
delete second;
delete third;
}
void Printname()
{
cout << first << " " << second << " " << third << endl;
}
};
class Person
{
private:
Name n;
string sex;
string national;
public:
Person(Name a, string sex, string national) :n(a), sex(sex), national(national)
{
cout << "構造完成锁荔!" << endl;
}
void printName()
{
n.Printname();
}
void printNational()
{
cout << national << endl;
}
};
int main()
{
Name name("比利王", "搞比利", "大xx");
Person s(name, "男", "日暮里");
s.printName();
s.printNational();
}
你拿去跑 如果你的電腦不報未加載wntdll (使用windows10 vs2019) 那我當場把這個電腦連鍵盤鼠標一起吃下去
那問題出在哪里呢?
在那個被我刪掉的復制構造函數(shù)體里面 新建Person對象時使用了其構造函數(shù)狸窘,這時候我們是使用對象name來為Person中n賦值的汞扎,這使得無論是傳遞給構造函數(shù)參數(shù)還是用name初始化n都調(diào)用了復制構造函數(shù)欲虚,而char*的默認復制是值傳遞作媚。也就是你只是復制了指針的內(nèi)容 并沒有重新開辟內(nèi)存
于是 在形參name傳遞給n之后 name被回收 調(diào)用析構函數(shù) 此時你的三個指針指向的內(nèi)容已經(jīng)去世了I形馈6舸摇!码泞!
更不用提main函數(shù)結束后兄旬,析構Person 再析構name 你的指針早就被除名了 你再強調(diào)去除這個指針指向的內(nèi)容(鬼知道他這個地址已經(jīng)被操作系統(tǒng)分配給誰了)就出現(xiàn)了上述的錯誤
修改也很簡單
#include<iostream>
#include<ctime>
#include<fstream>
#include<string>
#include<stack>
#include<vector>
#include<map>
using namespace std;
class Name {
private:
char* first;
char* second;
char* third;
public:
Name(string f = "", string s = "", string t = "")
{
first = new char[f.length() + 1];
second = new char[s.length() + 1];
third = new char[t.length() + 1];
strcpy_s(first, f.length() + 1, f.c_str());
strcpy_s(second, s.length() + 1, s.c_str());
strcpy_s(third, t.length() + 1, t.c_str());
}
Name(Name& name)
{
cout << "調(diào)用復制構造函數(shù)" << endl;
int length1 = strlen(name.first);
int length2 = strlen(name.second);
int length3 = strlen(name.third);
first = new char[length1+1];
second = new char[length2+1];
third = new char[length3+1];
strcpy_s(first, length1+1,name.first);
strcpy_s(second, length2+1,name.second);
strcpy_s(third,length3+1,name.third);
}
~Name()
{
cout << "發(fā)病了" << endl;
delete first;
delete second;
delete third;
}
void Printname()
{
cout << first << " " << second << " " << third << endl;
}
};
class Person
{
private:
Name n;
string sex;
string national;
public:
Person(Name a, string sex, string national) :n(a), sex(sex), national(national)
{
cout << "構造完成!" << endl;
}
void printName()
{
n.Printname();
}
void printNational()
{
cout << national << endl;
}
};
int main()
{
Name name("比利王", "搞比利", "大xx");
Person s(name, "男", "日暮里");
s.printName();
s.printNational();
}