C++程序问题,求帮忙修改。。

2025-05-12 02:07:39
推荐回答(1个)
回答(1):

当前错误:类定义末尾没加分号;

重要错误:构造函数中地址赋值,导致地址写无法完成。

#include "iostream"
using namespace std;
class Zfc
{
char *ptr;
public:
Zfc(char *p)
{
    int i=Len(p);
    ptr=(char *)malloc(i+1);
    for(int j=0;j        *(ptr+j)=*(p+j);
    *(ptr+i)=0;
}  //构造函数
char Change(char n,int m); //实现功能,替换第m个字符为n
int Len(char *p);         //计算字符串长度
void Display();
~Zfc(){free(ptr);}
};
char Zfc::Change(char n,int m)
{
*(ptr+m)=n;
return n;
}
int Zfc::Len(char *p)
{
int L=0;
char * q = p;
while(*q++)
L++;
return L;
}
void Zfc::Display()
{
cout<<"该字符串为:"<cout<<"该字符串长度为:"<}
void main()
{
    Zfc a1("hellow ward");
    a1.Display();
    a1.Change('o',9);
    a1.Display();
}