哪位高手帮我看看以下程序编译通不过,显示内存不能"written"。什么原因?先谢谢了。
//Employee.h
#ifndef EMPLOYEE
#define EMPLOYEE
class Name{
public:
Name(char *pName);
~Name();
protected:
char *surname;
char *name;
};
class Employee{
public:
Employee(char *pName);
~Employee();
protected:
Name who;
char street[10];
char city[10];
char province[10];
char post[10];
};
#endif
//Employee.cpp
#include "Employee.h "
#include <iostream.h>
#include <string.h>
Name::Name(char *pName)
{
char *str1= " ";
char *pName1;
char *ptr=strchr(pName, ' ');
strncpy(surname,pName,ptr-pName);
strcpy(pName1,pName);
strxfrm(pName1,str1,ptr-pName);
strcpy(name,pName1);
cout < < "constructing allname. " < <
"surname: " < <surname < < " name: " < <name < <endl;
}
Name::~Name()
{
cout < < "destructing a list " < <endl;
}
Employee::Employee(char *pName):who(pName)
{
cout < < "constructing a list. " < <endl;
strcpy(street, "xinhua ");
strcpy(city, "LangFang ");
strcpy(province, "HeBei ");
strcpy(post, "065000 ");
}
Employee::~Employee()
{
cout < < "Destructing a list1 " < <endl;
}
//mainfunc.cpp
#include "Employee.h "
#include <iostream.h>
int main()
{
Name who( "zhang Li ");
Employee person( "Liu San ");
return 0;
}
[解决办法]
字符指针还没分配空间当然会出错
指针都没new!!!
[解决办法]
这种错误多半都是因为对非法内存进行了 "写 "操作引起的.
正如楼上所说,你并没有给指针分配空间,就对它所指向的内存地址进行了 "写 ",
如果这个指针指的是一个非法的地址,就要报错