这段代码的最后那条语句会出现内存不能读的异常,是不是我的链表有问题呢?#include <iostream.h>
#include <string.h>class parents
{
public:
inline parents();
parents(char *str);
void virtual display();
static parents *pHead;
parents *pNext;
protected:
char string1[30];
public:
virtual ~parents();
};class child : public parents
{
public:
inline child();
child(char *, char *);
void virtual display();
private:
char string2[20];
public:
virtual ~child();
};parents *parents::pHead=0;parents::parents()
{
cout << "parents default construction" << endl;
}parents::~parents()
{
}parents::parents(char *str)
{
cout << "parents construction" << endl;
strcpy(string1,str);
if ( pHead==NULL )
pHead=this;
else
{ cout << "constrcuting link list" << endl;
child *pS;
pS=(child *)pHead;
while ( pS!= NULL ) pS=(child*)pS->pNext;
pS=(child*)this;
}
this->pNext=NULL;
}void parents::display()
{
cout << this->string1 << endl;
}child::child()
{
cout << "child default construction" << endl;
}
child::child(char *str1, char *str2) : parents( str1 )
{
cout << "child construction" << endl;
strcpy(string2,str2);
}
void child::display()
{
cout << this->string1 << endl;
cout << this->string2 << endl;
}child::~child()
{
}void main()
{
parents::pHead=NULL;
parents test("test string");
test.display();
child children("old", "young");
children.display();
parents::pHead->display();// 下面这条语句出现内存不能读的问题
((child *)test.pNext)->display();
}
#include <string.h>class parents
{
public:
inline parents();
parents(char *str);
void virtual display();
static parents *pHead;
parents *pNext;
protected:
char string1[30];
public:
virtual ~parents();
};class child : public parents
{
public:
inline child();
child(char *, char *);
void virtual display();
private:
char string2[20];
public:
virtual ~child();
};parents *parents::pHead=0;parents::parents()
{
cout << "parents default construction" << endl;
}parents::~parents()
{
}parents::parents(char *str)
{
cout << "parents construction" << endl;
strcpy(string1,str);
if ( pHead==NULL )
pHead=this;
else
{ cout << "constrcuting link list" << endl;
child *pS;
pS=(child *)pHead;
while ( pS!= NULL ) pS=(child*)pS->pNext;
pS=(child*)this;
}
this->pNext=NULL;
}void parents::display()
{
cout << this->string1 << endl;
}child::child()
{
cout << "child default construction" << endl;
}
child::child(char *str1, char *str2) : parents( str1 )
{
cout << "child construction" << endl;
strcpy(string2,str2);
}
void child::display()
{
cout << this->string1 << endl;
cout << this->string2 << endl;
}child::~child()
{
}void main()
{
parents::pHead=NULL;
parents test("test string");
test.display();
child children("old", "young");
children.display();
parents::pHead->display();// 下面这条语句出现内存不能读的问题
((child *)test.pNext)->display();
}
解决方案 »
- 得到指针
- 关于线程同步,讲得好再加分。
- 一个简单的 WINDOWS API函数,小女子求救!
- [请问]MFC基于Dialog如何得到屏幕右下角坐标(可用位置)
- tcp/ip通信中,如何将所选的文件,发送给服务器(类似QQ的文件传送)
- 父对话框上有个子对话框,子对话框用Onpaint()显示图片,父对话框弹出MessageBox被子对话框的显示图片部分遮住了,怎么办?
- 大伙来总结:在32位机上写的程序,转到64位机需要注意什么,做什么修改?
- 急:为什么GetProcAddress无法得到函数地址?
- 能否用socket传递记录集,请指教
- 关于重载CStatic类遇到的一点小问题!(分多的是!)
- ?????高分求解?????怎样在clistctrl里自动选择子项???????
- 为什么想在这里找个项目做做都找不到?给我项目做吧,我给高分!
//这句话导致test.pNext为空,函数调用失败
child *pS;
pS=(child *)pHead;
while ( pS!= NULL && pS->pNext != NULL)
{
pS=(child*)pS->pNext;
}
if(pS != NULL)
{
pS->pNext = (child*)this;
}
child *pS;
pS=(child *)pHead;
while ( pS!= NULL && pS->pNext != NULL)
{
pS=(child*)pS->pNext;
}
if(pS != NULL)
{
pS->pNext = (child*)this;
}