问题描述
- 请教大神! 这个程序出现的问题怎解决?
-
class CMessage
{
private:
char* m_pmessage;
public:
CMessage (const char* text="Default message")
{
size_t len=(strlen(text)+1);
m_pmessage=new char[len];
strcpy_s(m_pmessage,len,text);
}
CMessage (const CMessage & A)
{
size_t len=strlen((A.m_pmessage)+1);
m_pmessage=new char[len];
strcpy_s(m_pmessage,len,A.m_pmessage);
}
CMessage &operator=(const CMessage &A)
{
if(this!=&A)
{
delete[] m_pmessage;
size_t len=strlen((A.m_pmessage)+1);
m_pmessage=new char[len+len];
strcpy_s(this->m_pmessage,len,A.m_pmessage);
}
return this;
}
~CMessage()
{
cout<<"destructor called "<<endl;
delete []m_pmessage;
}
void reset()
{
char tempt=m_pmessage;
while(*tempt)
(tempt++)= '';
}
void show()
{
cout<<"m_pmessage"<<endl;
}
};
int main()
{
CMessage motto1("the dev takes care of his own");
CMessage motto2;
motto2.show();motto2=motto1; motto2.show(); motto1.reset(); motto1.show(); motto2.show(); return 0; }
解决方案
什么错误,什么编译器i
这代码需要vs2010以上编译
解决方案二:
size_t len=strlen((A.m_pmessage)+1);
->
size_t len = strlen(A.m_pmessage) + 1;
你括号打错了位置。
时间: 2024-10-18 12:30:02