解决这个问题的原理是什么?为什么在vc++上出错?
#include<iostream>
#include<string.h>
using namespace std;
class Merchandise{
public:
Merchandise(){};
~Merchandise(){};
friend istream &operator >> (istream &in,Merchandise &s);
friend ostream &operator << (ostream &out,Merchandise &s);
private:
int no;
char *name;
double price;
};
istream& operator >>(istream &in,Merchandise &s) //对<<进行重载
{
char temp[10];
s.name=new char[strlen(temp)+1];
cout<<"please input commoditiy's no:"<<endl;
in>>s.no;
cout<<"please input commoditiy's name:"<<endl;
in>>temp;
strcpy(s.name,temp);
cout<<"please input commoditiy's price:"<<endl;
in>>s.price;
return in;
}
ostream& operator <<(ostream &out,Merchandise &s)
{
out<<"\t"<<"commoditiy's information"<<endl;
out<<"no"<<"\t"<<"name"<<"\t"<<"price"<<endl;
out<<s.no<<"\t"<<s.name<<"\t"<<s.price<<endl;
return out;
}
int main()
{ Merchandise mer;
cin>>mer;
cout<<mer;
return 0;
}
为什么在vc++6.0上运行的时候会报错?是什么问题?怎么修改?修改的原理是?
--------------------Configuration: 5 - Win32 Debug--------------------
Compiling...
12-5-2-4.cpp
g:\计算机语言练习\5\12-5-2-4.h(18) : error C2248: 'name' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(12) : see declaration of 'name'
g:\计算机语言练习\5\12-5-2-4.h(20) : error C2248: 'no' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(11) : see declaration of 'no'
g:\计算机语言练习\5\12-5-2-4.h(23) : error C2248: 'name' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(12) : see declaration of 'name'
g:\计算机语言练习\5\12-5-2-4.h(25) : error C2248: 'price' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(13) : see declaration of 'price'
g:\计算机语言练习\5\12-5-2-4.h(32) : error C2248: 'no' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(11) : see declaration of 'no'
g:\计算机语言练习\5\12-5-2-4.h(32) : error C2248: 'name' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(12) : see declaration of 'name'
g:\计算机语言练习\5\12-5-2-4.h(32) : error C2248: 'price' : cannot access private member declared in class 'Merchandise'
g:\计算机语言练习\5\12-5-2-4.h(13) : see declaration of 'price'
G:\计算机语言练习\5\12-5-2-4.cpp(6) : error C2593: 'operator >>' is ambiguous
G:\计算机语言练习\5\12-5-2-4.cpp(7) : error C2593: 'operator <<' is ambiguous
执行 cl.exe 时出错.
[解决办法]
#include<iostream>
#include<string.h>
using namespace std;
class Merchandise{
public:
Merchandise(){}
~Merchandise(){}
friend istream &operator >> (istream &in,Merchandise &s);
friend ostream &operator << (ostream &out,Merchandise &s);
private:
int no;
char *name;
double price;
};
istream& operator >>(istream &in,Merchandise &s) //对<<进行重载
{
char temp[10];
s.name=new char[strlen(temp)+1];
cout<<"please input commoditiy's no:"<<endl;
in>>s.no;
cout<<"please input commoditiy's name:"<<endl;
in>>temp;
strcpy(s.name,temp);
cout<<"please input commoditiy's price:"<<endl;
in>>s.price;
return in;
}
ostream& operator <<(ostream &out,Merchandise &s)
{
out<<"\t"<<"commoditiy's information"<<endl;
out<<"no"<<"\t"<<"name"<<"\t\t"<<"price"<<endl;
out<<s.no<<"\t"<<s.name<<"\t"<<s.price<<endl;
return out;
}
int main()
{
Merchandise mer;
cin>>mer;
cout<<mer;
return 0;
}
//输出结果:
[code=c]
please input commoditiy's no:
123456
please input commoditiy's name:
csdnc/c++
please input commoditiy's price:
1000
commoditiy's information
nonameprice
123456csdnc/c++1000