C++ VS提示未加載 wntdll.pdb

如果網(wǎng)上各種誤人子弟的方法可以解決問(wèn)題的話 我就不在這逼逼了

重點(diǎn):出現(xiàn)這個(gè)肯定是你的代碼的問(wèn)題 VS自己少東西的可能性幾乎為0

而問(wèn)題基本都出在指針的使用上 一般都是你調(diào)用你的指針指向了錯(cuò)誤的東西 / 你調(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 << "構(gòu)造完成!" << endl;
    }
    void printName()
    {
        n.Printname();
    }
    void printNational()
    {
        cout << national << endl;
    }
};

int main()
{
    Name name("比利王", "搞比利", "大xx");
    Person s(name, "男", "日暮里");
    s.printName();
    s.printNational();
}

你拿去跑 如果你的電腦不報(bào)未加載wntdll (使用windows10 vs2019) 那我當(dāng)場(chǎng)把這個(gè)電腦連鍵盤(pán)鼠標(biāo)一起吃下去

那問(wèn)題出在哪里呢?

在那個(gè)被我刪掉的復(fù)制構(gòu)造函數(shù)體里面 新建Person對(duì)象時(shí)使用了其構(gòu)造函數(shù),這時(shí)候我們是使用對(duì)象name來(lái)為Person中n賦值的,這使得無(wú)論是傳遞給構(gòu)造函數(shù)參數(shù)還是用name初始化n都調(diào)用了復(fù)制構(gòu)造函數(shù),而char*的默認(rèn)復(fù)制是值傳遞。也就是你只是復(fù)制了指針的內(nèi)容 并沒(méi)有重新開(kāi)辟內(nèi)存

于是 在形參name傳遞給n之后 name被回收 調(diào)用析構(gòu)函數(shù) 此時(shí)你的三個(gè)指針指向的內(nèi)容已經(jīng)去世了?。。?!

更不用提main函數(shù)結(jié)束后,析構(gòu)Person 再析構(gòu)name 你的指針早就被除名了 你再?gòu)?qiáng)調(diào)去除這個(gè)指針指向的內(nèi)容(鬼知道他這個(gè)地址已經(jīng)被操作系統(tǒng)分配給誰(shuí)了)就出現(xiàn)了上述的錯(cuò)誤

修改也很簡(jiǎ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)用復(fù)制構(gòu)造函數(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 << "構(gòu)造完成!" << endl;
    }
    void printName()
    {
        n.Printname();
    }
    void printNational()
    {
        cout << national << endl;
    }
};

int main()
{
    Name name("比利王", "搞比利", "大xx");
    Person s(name, "男", "日暮里");
    s.printName();
    s.printNational();
}

程序完美運(yùn)行

這個(gè)故事告訴我們 能用string 就不要用char* 來(lái)裝逼 因?yàn)楹芏嗳硕疾恢廊绻阊b逼失敗了,怎么指出你到底哪里裝錯(cuò)了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容