首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

解决这个有关问题的原理是什么?为什么在vc++上出错

2013-12-10 
解决这个问题的原理是什么?为什么在vc++上出错?#includeiostream#includestring.husing namespace std

解决这个问题的原理是什么?为什么在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


[/code]
[解决办法]
引用:
linux有没有推荐?

一直用ubuntu了,vim/g++/gdb/makefile
[解决办法]
纠正:
将头文件
#include <iosteam>
using namespace std;
改为
#include <iostream.h>

原理:
VC++并没有完全实现C++标准,它所提供的不带后缀的".h"的头文件不支持把双目运算符重载为友元函数,此为重点!但是VC++提供的旧形式的带后缀的".h"头文件依旧支持此项功能。所以如上修改你的头文件可使编译通过。   

热点排行