我创建了自己的类字符串,当我编译我的代码时它有两个属性 buff 来存储字符串和长度,但如果我使用它作为对象,我会得到很多错误错误的原因是什么,我该怎么做防止他们谢谢
#include <iostream>
using namespace std;
class String
{
private:
int length;
char *buff;
public:
String operator=(String &);
String();
String(String &);
String(char *);
int size(char *);
void copy(char *);
char getvalue(int);
char *getbuff(){return buff;}
void setindex(char,int);
int getlength();
void display();
~String();
};
String :: String()
{
length = 1;
buff = 0;
}
String :: String(String &temp)
{
length = size(temp.buff);
buff = new char[temp.length + 1];
copy(temp.buff);
}
String :: String(char *a)
{
length = size(a);
buff = new char [length + 1];
copy(a);
}
int String :: size(char *a)
{
int i;
for(i = 0;a[i] != '\0';i++)
{
}
return i;
}
void String :: copy(char *a)
{
delete []buff;
int i;
length = size(a);
buff = new char[length + 1];
for (i=0;i<length;i++)
{
buff[i] = a[i];
}
buff[i] = '\0';
}
char String :: getvalue(int index)
{
return buff[index];
}
void String :: setindex(char value,int index)
{
buff[index] = value;
}
int String :: getlength()
{
return length;
}
void String :: display()
{
for (int i = 0;i<length;i++)
cout << buff[i];
}
String :: ~String()
{
delete []buff;
}
String String :: operator=(String &temp)
{
copy(temp.buff);
return *this;
}
void main()
{
String a("r");
String b("ee");
b = a;
b.display();
}