报错信息:
未加载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()的用法详解