赋值函数为什么编译有错误呢???

dearlee_01 2006-08-24 04:13:35
摘自《高质量c++编程指南》
///////////////////h//////////////////////
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
/////////////////////////////cpp///////////////////////////
#include "String.h"

String::String(const char *str) { // 普通构造函数
if(str==NULL) {
m_data = new char[1];
*m_data = '\0';
} else {
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}

}

String::String(const String & other) { // 拷贝构造函数
// 允许操作other 的私有成员m_data
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);
}

String & String::operate =(const String &other) { // 赋值函数
// (1) 检查自赋值
if(this == &other)
return *this;
// (2) 释放原有的内存资源
delete [] m_data;
// (3) 分配新的内存资源,并复制内容
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);
// (4) 返回本对象的引用
return *this;
}

String::~String() { // 析构函数
delete [] m_data; // 由于m_data 是内部数据类型,也可以写成 delete m_data;
}
...全文
332 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dearlee_01 2006-08-24
  • 打赏
  • 举报
回复
马虎了:)
archim 2006-08-24
  • 打赏
  • 举报
回复
operator是c++的关键字,operate不是
bohlee 2006-08-24
  • 打赏
  • 举报
回复
operate错误
archim 2006-08-24
  • 打赏
  • 举报
回复
operate --> operator
sinall 2006-08-24
  • 打赏
  • 举报
回复
#include <string.h>
///////////////////h//////////////////////
class String
{
public:
String(const char *str = NULL); // ÆÕͨ¹¹Ô캯Êý
String(const String &other); // ¿½±´¹¹Ô캯Êý
~String(void); // Îö¹¹º¯Êý
String& operator =(const String &other); // ¸³Öµº¯Êý
private:
char *m_data; // ÓÃÓÚ±£´æ×Ö·û´®
};
/////////////////////////////cpp///////////////////////////
//#include "String.h"

String::String(const char *str) { // ÆÕͨ¹¹Ô캯Êý
if(str==NULL) {
m_data = new char[1];
*m_data = '\0';
} else {
int length = strlen(str);
m_data = new char[length+1];
strcpy(m_data, str);
}

}

String::String(const String & other) { // ¿½±´¹¹Ô캯Êý
// ÔÊÐí²Ù×÷other µÄ˽ÓгÉÔ±m_data
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);
}

String & String::operator =(const String &other) { // ¸³Öµº¯Êý
// (1) ¼ì²é×Ô¸³Öµ
if(this == &other)
return *this;
// (2) ÊÍ·ÅÔ­ÓеÄÄÚ´æ×ÊÔ´
delete [] m_data;
// (3) ·ÖÅäеÄÄÚ´æ×ÊÔ´£¬²¢¸´ÖÆÄÚÈÝ
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);
// (4) ·µ»Ø±¾¶ÔÏóµÄÒýÓÃ
return *this;
}

String::~String() { // Îö¹¹º¯Êý
delete [] m_data; // ÓÉÓÚm_data ÊÇÄÚ²¿Êý¾ÝÀàÐÍ£¬Ò²¿ÉÒÔд³É delete m_data;
}

int main(void)
{
return 0;
}
sinall 2006-08-24
  • 打赏
  • 举报
回复
String & operator =(const String &other); // 赋值函数

String & String::operator =(const String &other) { // 赋值函数
// (1) 检查自赋值
if(this == &other)
return *this;
// (2) 释放原有的内存资源
delete [] m_data;
// (3) 分配新的内存资源,并复制内容
int length = strlen(other.m_data);
m_data = new char[length+1];
strcpy(m_data, other.m_data);
// (4) 返回本对象的引用
return *this;
}
dearlee_01 2006-08-24
  • 打赏
  • 举报
回复
d:\developtools\microsoft visual studio\myprojects\testcstring\string.h(9) : error C2146: syntax error : missing ')' before identifier 'other'
d:\developtools\microsoft visual studio\myprojects\testcstring\string.h(9) : error C2065: 'other' : undeclared identifier
d:\developtools\microsoft visual studio\myprojects\testcstring\string.h(9) : error C2059: syntax error : ')'
d:\developtools\microsoft visual studio\myprojects\testcstring\string.h(9) : error C2258: illegal pure syntax, must be '= 0'
d:\developtools\microsoft visual studio\myprojects\testcstring\string.h(9) : error C2252: 'operate' : pure specifier can only be specified for functions
Error executing cl.exe.

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧