| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 687 人关注过本帖
标题:[求助]请教菜鸟问题!
只看楼主 加入收藏
yylcel
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2005-12-10
收藏
 问题点数:0 回复次数:9 
[求助]请教菜鸟问题!

#include<iostream>
#include<string>
using namespace std;
class String
{
public:
String(){p=NULL;}
String(char *str);
void display();
friend bool operator>(String &string1,String &string2);
private:
char *p;
};
String::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
bool operator>(String &string1,String &string2)
{
if(strcmp(string1.p,string2.p)>0)
return false;
else return true;
}
int main()
{
String string1("hello!"),string2("book!");
cout<<(string1>string2)<<endl;
return 0;
}
--------------------Configuration: vetor1 - Win32 Debug--------------------
Compiling...
vetor1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(24) : error C2248: 'p' : cannot access private member declared in class 'String'
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(12) : see declaration of 'p'
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(24) : error C2248: 'p' : cannot access private member declared in class 'String'
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(12) : see declaration of 'p'
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(31) : error C2593: 'operator >' is ambiguous
Error executing cl.exe.

vetor1.exe - 3 error(s), 0 warning(s)

搜索更多相关主题的帖子: private display include public friend 
2006-03-30 21:52
yylcel
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2005-12-10
收藏
得分:0 
晕呐没人...自己顶了~~~~急!!!!!!!!!!!
2006-03-30 22:27
皋阳
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-3-28
收藏
得分:0 

#include<iostream>
#include<string>
using namespace std;
class String
{
public:
String(){p=NULL;}
String(char *str);
void display();
friend bool operator>(String &string2);
private:
char *p;
};
String::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
bool operator>(String &string2)
{
if(strcmp(p,string2.p)>0)
return false;
else return true;
}
int main()
{
String string1("hello!"),string2("book!");
cout<<(string1>string2)<<endl;
return 0;
}
注意重载的运算符的操作数的个数!


2006-03-30 22:45
yylcel
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2005-12-10
收藏
得分:0 

--------------------Configuration: vetor1 - Win32 Debug--------------------
Compiling...
vetor1.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(10) : error C2805: binary 'operator >' has too few parameters
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(23) : error C2805: binary 'operator >' has too few parameters
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(24) : error C2065: 'p' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(24) : error C2248: 'p' : cannot access private member declared in class 'String'
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(12) : see declaration of 'p'
C:\Program Files\Microsoft Visual Studio\MyProjects\vetor1\vetor1.cpp(31) : error C2676: binary '>' : 'class String' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

vetor1.obj - 5 error(s), 0 warning(s)
运行楼上的~~~~

2006-03-31 18:26
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
把3楼的改动的两行按下面的改法,变成member function 就可以了。
bool operator>(String &string2);
bool String::operator>(String &string2)

http://myajax95./
2006-03-31 19:23
皋阳
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-3-28
收藏
得分:0 

晕,是要像楼上那样再改下才行。

[此贴子已经被作者于2006-3-31 19:56:03编辑过]


2006-03-31 19:52
yylcel
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2005-12-10
收藏
得分:0 
那友元函数干嘛不行啊?怎么会不能访问呢error C2248: 'p' : cannot access private member declared in class 'String'
2006-03-31 21:02
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
带一个参数的operator overloading对于“>”来说只能是member function。
如果想用friend function的">"就得带两个参数。那样作也可以。

http://myajax95./
2006-04-01 00:59
lianxinkai
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2006-3-3
收藏
得分:0 
我思考思考再给你说'

在知识的海洋中我永远得不到满足!
不过也喜欢玩游戏
2006-04-01 12:36
yylcel
Rank: 1
等 级:新手上路
帖 子:39
专家分:0
注 册:2005-12-10
收藏
得分:0 
谢谢楼上的几位....
2006-04-01 13:06
快速回复:[求助]请教菜鸟问题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017208 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved