#ifndef ASTRING_H
#define ASTRING_H
#define defaultSize 128//////字符串的最大长度
#include<iostream>
using namespace std;
class AString {
char* ch;
int curLength;
int maxSize;
public:
AString(int sz = defaultSize);
AString(const char* init);
AString(const AString& ob);
~AString() { delete[]ch; };
int Length()const { return curLength; }
AString& operator()(int pos, int len);
bool operator == (AString&ob)const
{
return strcmp(ch, ob.ch) == 0;
}
bool operator!=(AString&ob)const
{
return strcmp(ch, ob.ch) != 0;
}
bool operator!()const
{
return curLength == 0;
}
AString& operator=(AString& ob);
AString& operator+=(AString& ob);
char& operator[](int i);
int Find(AString& pat, int k)const;
};
AString::AString(int sz)
{
maxSize = sz;
ch = new char[maxSize + 1];
if (ch == NULL)
{
cerr << "存储分配错!\n"; exit(1);
}
curLength = 0;
ch[0] = '\0';
};
AString::AString(const char* init)
{
int len = strlen(init);
maxSize = (len > defaultSize) ? len : defaultSize;
ch = new char[maxSize + 1];
if (ch == NULL)
{
cerr << "存储分配出错!\n"; exit(1);
}
curLength = len;
strcpy_s(ch, sizeof(init) + 1, init);
};
AString::AString(const AString& ob)
{
maxSize = ob.maxSize;
ch = new char[maxSize + 1];
if (ch == NULL)
{
cerr << "存储分配出错!\n"; exit(1);
}
curLength = ob.curLength;
strcpy_s(ch, sizeof(ob.ch) + 1, ob.ch);
};
AString& AString::operator=(const AString& ob)
{
if (&ob != this)
{
delete[]ch;
maxSize = ob.maxSize;
ch = new char[ob.maxSize + 1];
if (ch == NULL)
{
cerr << "存储分配失败!\n"; exit(1);
}
curLength = ob.curLength;
strcpy_s(ch, sizeof(ob.ch) + 1, ob.ch);
}
else
cout << "字符串自身赋值出错!\n";
return *this;
};
#endif
AString& operator=(AString& ob);
AString& AString::operator=(const AString& ob)
你这个=重载申明和函数体的定义写的不一样,函数体定义中参数多了const。把const去掉即可
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632