奇怪!重载运算符的友元函数为什么识别不了?
<————源程序————>//类OpoverClass定义头文件OpoverClass.h
#ifndef H_OpoverClass
#define H_OpoverClass
#include <iostream>
using namespace std;
class OpoverClass
{
friend ostream& operator<<(ostream&,const OpoverClass&);
friend istream& operator>>(istream&,OpoverClass&);
public:
OpoverClass operator+ (const OpoverClass&) const;
OpoverClass operator* (const OpoverClass&) const;
OpoverClass (int i=0,int j=0)
{
a=i;b=j;
};
private:
int a;
int b;
};
#endif
//类OpoverClass的实现文件OpoverClassImp.cpp
#include <iostream> //line1
#include "OpoverClass.h" //line2
using namespace std; //line4
ostream& operator<<(ostream& osObject,const OpoverClass& right)
{
osObject<< "(" << right.a << "," << right.b << ")"; //line8
return osObject;
}
istream& operator>>(istream& isObject,OpoverClass& right) //line13
{
isObject>>right.a>>right.b; //line15
return isObject;
}
OpoverClass OpoverClass::operator *(const OpoverClass& right) const
{
OpoverClass temp;
temp.a=a*right.a;
temp.b=b*right.b;
return temp;
}
OpoverClass OpoverClass::operator +(const OpoverClass& right) const
{
OpoverClass temp;
temp.a=a+right.a;
temp.b=b+right.b;
return temp;
}
//主程序mainProgram.cpp
#include <iostream>
#include "OpoverClass.h"
using namespace std;
int main()
{
OpoverClass u(23,46);
OpoverClass v;
cout<<"u="<<u<<endl;
cout<<"Enter two integers:";
cin>>v;
cout<<endl;
cout<<"u+v="<<u+v<<endl;
cout<<"u*v="<<u*v<<endl;
return 0;
}
在VC++6.0上编译时产生如下错误:
Compiling...
OpoverClassImp.cpp
E:\VC_Examples\E12\OpoverClassImp.cpp(8) : error C2248: 'a' : cannot access private member declared in class 'OpoverClass'
e:\vc_examples\e12\opoverclass.h(20) : see declaration of 'a'
E:\VC_Examples\E12\OpoverClassImp.cpp(8) : error C2248: 'b' : cannot access private member declared in class 'OpoverClass'
e:\vc_examples\e12\opoverclass.h(21) : see declaration of 'b'
E:\VC_Examples\E12\OpoverClassImp.cpp(15) : error C2248: 'a' : cannot access private member declared in class 'OpoverClass'
e:\vc_examples\e12\opoverclass.h(20) : see declaration of 'a'
E:\VC_Examples\E12\OpoverClassImp.cpp(15) : error C2248: 'b' : cannot access private member declared in class 'OpoverClass'
e:\vc_examples\e12\opoverclass.h(21) : see declaration of 'b'
Error executing cl.exe.
E12.exe - 4 error(s), 0 warning(s)
望高手给以指点!不胜感激!