读书人

Cstring类中怎么重载强制类型转换?下

发布时间: 2014-01-03 00:30:09 作者: rapoo

Cstring类中如何重载强制类型转换?下面的程序请求大神指点


char *string::operator const char *()const//到普通c字符串的转换;
{
return this->buffer;
}

vc6.0中显示的错误是:
1.error C2549: user-defined conversion cannot specify a return type
2.error C2586: incorrect user-defined conversion syntax : illegal indirections
3.error C2556: '__thiscall string::operator`const char ** '(void) const' : overloaded function differs only by return type from '__thiscall string::operator`const char *'(void) const'

类的定义如下:

class string
{
private:
unsigned buflen;
char *buffer;
public:
string();//构造函数;
string(unsigned length);
string(const char *);
string(const string &);//复制构造函数;
~string();//析构函数;

//重载运算符
string& operator = (const string &right);
string& operator += (const string &right);
char *operator += (const char *right);

friend string operator + (const string &left, const string &right);//字符串连接;

string operator()(unsigned start, unsigned len);//取子串;
unsigned length()const;//求字符串长度;
char & operator[](unsigned index)const;//访问单个字符;
int compare(const string &)const;//字符串比较;
operator const char * ()const;//到普通字符串的转换;
friend ostream & operator<<(ostream &, string &);//字符串的输出;
};

[解决办法]
去掉前面的char*
[解决办法]
user-defined conversion cannot specify a return type
用户自定义的转换不能指定返回类型


string::operator const char *()const//到普通c字符串的转换;


哎,谁说学编程英语无所谓的?

读书人网 >C++

热点推荐