C++ VS2015提示未加载 wntdll.pdb

报错信息:

未加载wntdll.pdb,下面的框架可能不正确/或缺失,没有为ntdll.dll加载符号。

出错原因:
你的指针指向了错误的东西 / 你调用你的指针释放了奇怪的内容,和内存的调用有很大的关系,比如new的使用。

案例:

有问题的代码:

#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 << "发病了" << 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();
}

那问题出在哪里呢?
在那个被我删掉的复制构造函数体里面 新建Person对象时使用了其构造函数,这时候我们是使用对象name来为Person中n赋值的,这使得无论是传递给构造函数参数还是用name初始化n都调用了复制构造函数,而char*的默认复制是值传递。也就是你只是复制了指针的内容 并没有重新开辟内存
于是 在形参name传递给n之后 name被回收 调用析构函数 此时你的三个指针指向的内容已经没有了。

修改之后,使用strcpy_s来安全拷贝字符串:

#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 << "调用复制构造函数" << 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 << "发病了" << 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();
}

或者使用深拷贝函数:


#include <string>
 
void string2char(std::string src, char *dest) {
    memcpy(dest, src.c_str(), strlen(src.c_str()));
}
 
int main() {
    std::string str = "10.66.38.214:1522/orcl";
    char szDest[256] = { 0 };
 
    string2char(str, szDest);
}

c_str():生成一个const char*指针,指向以空字符终止的数组。string–>char*

参考链接:
1.C++ VS提示未加载 wntdll.pdb
2. c++中c_str()的用法详解

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值