请教高手!!!!!我快疯掉了(关于重载==)

usherhll 2009-12-26 09:45:27
写了个测试的程序,奇怪的是老是抛出错误,求助高手:错误如下:
cannot access private member declared in class 'String'
D:\cpp\oostring\test\test.cpp(10) : see declaration of 'p'
D:\cpp\oostring\test\test.cpp(20) : error C2248: 'p' : cannot access private member declared in class 'String'
D:\cpp\oostring\test\test.cpp(10) : see declaration of 'p'
D:\cpp\oostring\test\test.cpp(29) : error C2593: 'operator ==' is ambiguous
Error executing cl.exe.
如果是+,*之类的就没问题,为什么呢??!!!!!!
下面是源代码:
在线等,不胜感激!!

#include <iostream>
using namespace std;
class String
{
public:String(){p=NULL;}
String(char *str);
friend bool operator ==( String &string1,String &string2);
void display();
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 true;
else
return false;
}
int main()
{
String string1("Hello");
String string2("Hello");
cout<<(string1==string2)<<endl;
return 0;
}






...全文
105 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
xparmenides 2009-12-26
  • 打赏
  • 举报
回复
"cout < <"改为"cout <<"
huang_gen 2009-12-26
  • 打赏
  • 举报
回复
//虽然我不是高手,但是在vc下调试通过,如果对你问题的解决有帮组,记得结账喔,红色为我的修改
#include <iostream>
//using namespace std; 可能是你的==重载和std命名空间中的全局operator==
冲突的缘故,还有你的程序有一个致命的错误,就是p指向了一个临时的变量。和我开始的时候犯一样的错


class String
{
public:String(){p=NULL;}
String(char *str);
friend bool operator==( String const&string1,String const& string2);
void display();
private:
char *p;
};
String::String(char *str)
{
p=str;}
void String::display()
{
std::cout <<p;}这样使用

bool operator==( String const&string1,String const&string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}

int main()
{
String string1("Hello");
String string2("Hello");
std::cout <<(string1==string2) <<std::endl;
return 0;
}


usherhll 2009-12-26
  • 打赏
  • 举报
回复
我在vc6.0下会报错,很无语
最后改成了内部函数,就可以了。。。。。谢谢大家,特别是阿荣
mstlq 2009-12-26
  • 打赏
  • 举报
回复
标准下没问题的版本……
vc6不保证……

#include <iostream>
using namespace std;

class String
{
public:String(){p=NULL;}
String(char *str);
friend bool operator ==( const String &string1,const String &string2);
void display();
private:
char *p;
};
String::String(char *str)
{
p=str;}
void String::display()
{
cout <<p;}
bool operator ==( const String &string1,const String &string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}

int main()
{
String string1("Hello");
String string2("Hello");
cout <<(string1==string2) <<endl;
return 0;
}
usherhll 2009-12-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 arong1234 的回复:]
应该只在第一个参数不是当前类型使用友元,你这种情况应该使用成员函数

对于这个函数,String&应该使用const String&

[/Quote]
我加了const, 好像还是那个错误
昵称很不好取 2009-12-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 thefirstz 的回复:]
我这和楼上恰恰相反,VC6.0下报错,其余编译器都没有问题,输出1
[/Quote]
晕,大伙回的好快,楼上指的是2楼~~
arong1234 2009-12-26
  • 打赏
  • 举报
回复
应该只在第一个参数不是当前类型使用友元,你这种情况应该使用成员函数

对于这个函数,String&应该使用const String&
usherhll 2009-12-26
  • 打赏
  • 举报
回复
我的也是vc 6.0啊,怎么回事。。。。
昵称很不好取 2009-12-26
  • 打赏
  • 举报
回复
我这和楼上恰恰相反,VC6.0下报错,其余编译器都没有问题,输出1
  • 打赏
  • 举报
回复
vs2008直接编译通过

参数都加上const 试试
fox000002 2009-12-26
  • 打赏
  • 举报
回复
写法没有错误吧

不知 lz 用什么编译的
cattycat 2009-12-26
  • 打赏
  • 举报
回复
我这没有问题啊,vc6.0,正确,可以执行啊。
重贴一下你的代码。
#include <iostream> 
using namespace std;

class String
{
public:String(){p=NULL;}
String(char *str);
friend bool operator ==( String &string1,String &string2);
void display();
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 true;
else
return false;
}

int main()
{
String string1("Hello");
String string2("Hello");
cout <<(string1==string2) <<endl;
return 0;
}
usherhll 2009-12-26
  • 打赏
  • 举报
回复
自己做沙发。。。人呢

65,210

社区成员

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

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