c++为何我声明了友元,但编译器还是提示:cannot access private member declared in class 'book'

ResearchWorld 2012-07-26 06:29:21

#include <iostream>
#include <string>
using namespace std;

class book
{
public:

book():bookname(" "),number(0),money(0.0){}
book(string& dbookname,int dnumber,double dmoney):bookname(dbookname),number(dnumber),money(dmoney){}

friend istream& operator>>(istream& in,book& inbook);
friend ostream& operator<<(ostream& put,book& putbook);
book& operator+(book& b1)
{
//book result;

bookname+=b1.bookname;
number+=b1.number;
money+=b1.money;
return *this;
}
private:
string bookname;
int number;
double money;


};
istream& operator>>(istream& in,book& inbook)
{
cout<<"Please Enter bookname"<<endl;
in>>inbook.bookname;
in.clear();
cout<<"how much"<<endl;
in>>inbook.number;
in.clear();
cout<<"how money"<<endl;
in>>inbook.money;
in.clear();
return in;
}

ostream& operator<<(ostream& put,book& putbook)
{
put<<"the bookname:"<<putbook.bookname<<endl;
put<<"the number :"<<putbook.number<<endl;
put<<"how money :"<<putbook.money<<endl;
return put;
}

int main()
{
book a("Hacker's story",2,89.56);
book b("c++primer",1,68.2);
book c;
c=a+b;
cout<<c;
return 0;
}
...全文
532 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
cbzjzsb123 2012-07-27
  • 打赏
  • 举报
回复
VC6对STL的支持不好,这是大家公认的。所以如果楼主要研究和STL相关的东西,建议你装VS2005或以后的版本吧。
pathuang68 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

而且现在按照网上的方法 ,搞得我office都用不了额 ,我 晕呀。
[/Quote]

VC6对STL的支持不好,这是大家公认的。所以如果楼主要研究和STL相关的东西,建议你装VS2005或以后的版本吧。
Flammable_ice 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
如果是用VC,将友元函数定义放到相应类中!
[/Quote]
++
说得没错,在VC++6.0编译通过了。

#include <iostream>
#include <string>
using namespace std;
class book
{
public:

book():bookname(" "),number(0),money(0.0){}
book(string& dbookname,int dnumber,double dmoney):bookname(dbookname),number(dnumber),money(dmoney){}

friend istream& operator>>(istream& in,book& inbook)
{
cout<<"Please Enter bookname"<<endl;
in>>inbook.bookname;
in.clear();
cout<<"how much"<<endl;
in>>inbook.number;
in.clear();
cout<<"how money"<<endl;
in>>inbook.money;
in.clear();
return in;
}
friend ostream& operator<<(ostream& put,book& putbook)
{
put<<"the bookname:"<<putbook.bookname<<endl;
put<<"the number :"<<putbook.number<<endl;
put<<"how money :"<<putbook.money<<endl;
return put;
}
book& operator+(book& b1)
{
//book result;

bookname+=b1.bookname;
number+=b1.number;
money+=b1.money;
return *this;
}
private:
string bookname;
int number;
double money;


};


int main()
{
string strName = "Hacker's story";
string strName1 = "c++primer";
book a(strName, 2, 89.56);
book b(strName1, 1, 68.2);
book c;
c=a+b;
cout<<c;
return 0;

ResearchWorld 2012-07-27
  • 打赏
  • 举报
回复
而且现在按照网上的方法 ,搞得我office都用不了额 ,我 晕呀。
ResearchWorld 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 的回复:]
那个.net framework 可以单独安装吧,先下载一个装上再装2008试试。我记得好像是3.5版本的。

引用 7 楼 的回复:
引用 5 楼 的回复:
楼主的代码在VS2010上貌似没啥问题...用的编译器?

我的编译器是VC6.0的。
昨天下了VS2010 可他丫的装在Microsoft .net framework4.0就失败了!
今天下了个 vs2008装也一样。也……
[/Quote]
没用啊 .net都不能单独安装
brunodl 2012-07-27
  • 打赏
  • 举报
回复
楼主,我也在VC6上遇到了这个问题,我用了两种方法感觉都可以,一是将头文件声明为#include<iostream.h>然后删除#include <iostream>和using namespace std;
另一种是在类定义前声明一个类,然后再声明你要定义成友元的函数,然后在类的定义中将函数声明成为友元,最后实现
翅膀又硬了 2012-07-27
  • 打赏
  • 举报
回复
那个.net framework 可以单独安装吧,先下载一个装上再装2008试试。我记得好像是3.5版本的。[Quote=引用 7 楼 的回复:]
引用 5 楼 的回复:
楼主的代码在VS2010上貌似没啥问题...用的编译器?

我的编译器是VC6.0的。
昨天下了VS2010 可他丫的装在Microsoft .net framework4.0就失败了!
今天下了个 vs2008装也一样。也是那个.net 的安装失败,我好无语哦,我不想重装系统!
[/Quote]
太上绝情 2012-07-27
  • 打赏
  • 举报
回复
应该没问题啊
linguangliang 2012-07-27
  • 打赏
  • 举报
回复
最后劝你将带参构造函数中string & 改成const string &,如下:
book(const string& dbookname,int dnumber,double dmoney):bookname(dbookname),number(dnumber),money(dmoney){}
linguangliang 2012-07-27
  • 打赏
  • 举报
回复
如果是用VC,将友元函数定义放到相应类中!
ljhhh0123 2012-07-27
  • 打赏
  • 举报
回复
导弹打蚊子.
练习纯C++,装个cygwin就ok了.何必用那盗版的vc.
cygwin g++ 3.4.4只改这三行就通过了.
string s1="Hacker's story",s2="c++primer";
book a(s1,2,89.56);
book b(s2,1,68.2);
ResearchWorld 2012-07-27
  • 打赏
  • 举报
回复
网上的好多方法 我都试了哦 ,没用
ResearchWorld 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
楼主,珍惜生命,远离VC6这个垃圾货。
[/Quote]
我也想啊,可是 vs08-10系列摸都不让我摸一下 他丫的总在装.net framework时 失败的 。
ResearchWorld 2012-07-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
楼主的代码在VS2010上貌似没啥问题...用的编译器?
[/Quote]
我的编译器是VC6.0的。
昨天下了VS2010 可他丫的装在Microsoft .net framework4.0就失败了!
今天下了个 vs2008装也一样。也是那个.net 的安装失败,我好无语哦,我不想重装系统!
mountain310 2012-07-27
  • 打赏
  • 举报
回复
试试去掉using namespace std;之后将cin cout 等对象之前加上作用域操作符和作用域std::
taodm 2012-07-26
  • 打赏
  • 举报
回复
楼主,珍惜生命,远离VC6这个垃圾货。
pathuang68 2012-07-26
  • 打赏
  • 举报
回复
楼主的代码在VS2010上貌似没啥问题...用的编译器?
ResearchWorld 2012-07-26
  • 打赏
  • 举报
回复
回复3楼
那代码 贴上去也是错的!
kingdom_0 2012-07-26
  • 打赏
  • 举报
回复
class book
{
public:

book():bookname(" "),number(0),money(0.0){}
book(string& dbookname,int dnumber,double dmoney):bookname(dbookname),number(dnumber),money(dmoney){}

friend istream& operator>>(istream& in,book& inbook);
friend ostream& operator<<(ostream& put,book& putbook);
book& operator+(book& b1)
{
//book result;

bookname+=b1.bookname;
number+=b1.number;
money+=b1.money;
return *this;
}
private:
string bookname;
int number;
double money;


};
istream& operator>>(istream& in,book& inbook)
{
cout<<"Please Enter bookname"<<endl;
in>>inbook.bookname;
in.clear();
cout<<"how much"<<endl;
in>>inbook.number;
in.clear();
cout<<"how money"<<endl;
in>>inbook.money;
in.clear();
return in;
}

ostream& operator<<(ostream& put,book& putbook)
{
put<<"the bookname:"<<putbook.bookname<<endl;
put<<"the number :"<<putbook.number<<endl;
put<<"how money :"<<putbook.money<<endl;
return put;
}


int _tmain(int argc, _TCHAR* argv[])
{
string strName = "Hacker's story";
string strName1 = "c++primer";
book a(strName, 2, 89.56);
book b(strName1, 1, 68.2);
book c;
c=a+b;
cout<<c;

system("pause");
return 0;
}
skyandcode 2012-07-26
  • 打赏
  • 举报
回复
跟我课本的例子错得一模一样。楼主知道答案回复我啊
加载更多回复(1)

64,636

社区成员

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

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