找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 458|回复: 8

很不明白的问题

[复制链接]
发表于 2009-11-4 02:22:27 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include <iostream>
#include <string>
using namespace std;
class String
{
private:
string s;

public:

String(string s1)
{
s=s1;
}
String()
{
}
~String()
{

}

void show()
{
cout<<s<<endl;
}

String operator+(String &);
friend void operator=(String &,String &);
friend int operator>(String &,String &);

};
String String::operator+(String &s1)
{
String t;
t.s=s1.s;
return t;
}


void operator=(String &s1,String &s2)
{
s1.s=s2.s;

}

int operator>(String &s1,String &s2)
{
if(s1.s>s2.s)
return 1;
else
return 0;
}


int main()
{
String str1("LOVE"),str2("you"),str3;
str1.show();
str2.show();
str3=str1+str2;
str3.show();
if(str1>str2)
cout<<"str1>str2"<<endl;
else
cout<<"str1<str2"<<endl;
getchar();
return 0;
}

出错:
32 D:\Dev-Cpp\未命名1.cpp
`void operator=(String&, String&)' must be a nonstatic member
47 D:\Dev-Cpp\未命名1.cpp
`void operator=(String&, String&)' must be a nonstatic member
发表于 2009-11-4 02:22:28 | 显示全部楼层 IP:江苏扬州
opertor=必需是成员函数
以下是MSDN中对此的描述
The assignment operator (=) is, strictly speaking, a binary operator. Its declaration is identical to any other binary operator, with the following exceptions:

●It must be a nonstatic member function. No operator= can be declared as a nonmember function.

●It is not inherited by derived classes.

●A default operator= function can be generated by the compiler for class types if none exists.
回复

使用道具 举报

发表于 2009-11-4 02:22:29 | 显示全部楼层 IP:江苏扬州
这题是书上后面的练习题
规定"=,>"运算符重载函数为友元函数,而“+”为运算符重载函数为成员函数
楼上的,可以用中文解释么?我英语太烂了
回复

使用道具 举报

发表于 2009-11-4 02:22:30 | 显示全部楼层 IP:江苏扬州
严格的说,赋值操作符是一个二元操作符.它的声明和其他的二元操作符一样,但是以下例外

●赋值操作符必需是非静态成员函数.opertor=不可以声明为非成员函数

●赋值操作符不会被派生类继承

●如果编译器找不到opertor=,就会为类生成一个缺省的opertor=函数

以上是我的翻译,不太准确。

如果书上非让你那么做,换本书吧
回复

使用道具 举报

发表于 2009-11-4 02:22:31 | 显示全部楼层 IP:江苏扬州
那本书,本来就是入门者学的,我也觉得垃圾,
不过,我只想把后面的练习全部做完,

谢谢!你噢
回复

使用道具 举报

发表于 2009-11-4 02:22:31 | 显示全部楼层 IP:江苏扬州
你写的那程序也太离谱了吧,,,
回复

使用道具 举报

发表于 2009-11-4 02:22:32 | 显示全部楼层 IP:江苏扬州
楼上的``指教一下啊
回复

使用道具 举报

发表于 2009-11-4 02:22:34 | 显示全部楼层 IP:江苏扬州
友员重载等号的参数要是常类型
回复

使用道具 举报

发表于 2009-11-4 02:22:35 | 显示全部楼层 IP:江苏扬州
The assignment operator (=) is, strictly speaking, a binary operator. Its declaration is identical to any other binary operator, with the following exceptions:
●It must be a nonstatic member function. No operator= can be declared as a nonmember function.
●It is not inherited by derived classes.
●A default operator= function can be generated by the compiler for class types if none exists.

这里面清楚的讲到:"No operator= can be declared as a nonmember function",即等于号的重载函数必须是(类)的成员函数。而你的程序里把它作为了友元函数,不符合语法要求。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 07:41 , Processed in 0.270102 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表