代码是下面这样的,main()只是测试一下程序,最关键的错误是我定义的友元函数(哪儿错了)为什么不能访问私有函数???求大神解释
郁闷一下午and一晚上了
//"force.h"
#ifndef FORCE_H_
#define FORCE_H_
#include<iostream>
namespace FORCE
{
class Force
{
private:
double magnitude;
double direction;
public:
Force(){magnitude=0,direction=0;};
Force(double,double);
~Force(){};
Force operator+(Force & f1)const;
Force operator-(Force & f1)const;
Force operator-()const;
Force operator*(double num)const;
double operator*(Force & f1)const;
Force operator/(double & num)const;
friend Force operator*(double num,Force & f1);
friend std::ostream &operator<<(std::ostream &os,Force &f1);
};
}
#endif
//文件forces
#include<iostream>
#include"force.h"
#include<cmath>
#define angle (atan(1.0)/45.0)
using namespace FORCE;
Force::Force(double a,double b)
{
magnitude=a;
direction=b*angle;
if(direction>180)
direction-=360;
else if(direction<-180)
direction+=360;
}
Force Force::operator+(Force & f1)const
{
Force f;
double x;
double y;
x=magnitude*cos(direction)+f1.magnitude*cos(f1.direction);
y=magnitude*sin(direction)+f1.magnitude*sin(f1.direction);
f.magnitude=sqrt(x*x+y*y);
f.direction=atan2(y,x);
return f;
}
Force Force::operator-(Force & f1)const
{
Force f;
double x;
double y;
x=abs(magnitude*cos(direction))-abs(f1.magnitude*cos(f1.direction));
y=abs(magnitude*sin(direction))-abs(f1.magnitude*sin(f1.direction));
f.magnitude=sqrt(x*x+y*y);
f.direction=atan2(y,x);
return f;
}
Force Force::operator-()const
{
return Force(magnitude,direction+180);
}
Force Force::operator*(double num)const
{
return Force(num*magnitude,direction);
}
double Force::operator*(Force & f1)const
{
return (magnitude*f1.magnitude*cos(direction-f1.direction));
}
Force Force::operator/(double & num)const
{
return Force(magnitude/num,direction);
}
Force operator*(double & num,Force & f1)
{
return f1*num;
}
std::ostream & operator<<(std::ostream &os,Force &f1)
{
os<<"the force's magnitude is "<<f1.magnitude*angle<<std::endl;
os<<"the force's direction is "<<f1.direction*angle<<std::endl;
}
//main()文件
#include<iostream>
#include"force.h"
using namespace FORCE;
int main()
{
Force f1(10,120),f2(10,120);
Force f3=4*f2;
cout<<f3;
}



//"force.h"
#ifndef FORCE_H_
#define FORCE_H_
#include<iostream>
namespace FORCE
{
class Force
{
private:
double magnitude;
double direction;
public:
Force(){magnitude=0,direction=0;};
Force(double,double);
~Force(){};
Force operator+(Force & f1)const;
Force operator-(Force & f1)const;
Force operator-()const;
Force operator*(double num)const;
double operator*(Force & f1)const;
Force operator/(double & num)const;
friend Force operator*(double num,Force & f1);
friend std::ostream &operator<<(std::ostream &os,Force &f1);
};
}
#endif
//文件forces
#include<iostream>
#include"force.h"
#include<cmath>
#define angle (atan(1.0)/45.0)
using namespace FORCE;
Force::Force(double a,double b)
{
magnitude=a;
direction=b*angle;
if(direction>180)
direction-=360;
else if(direction<-180)
direction+=360;
}
Force Force::operator+(Force & f1)const
{
Force f;
double x;
double y;
x=magnitude*cos(direction)+f1.magnitude*cos(f1.direction);
y=magnitude*sin(direction)+f1.magnitude*sin(f1.direction);
f.magnitude=sqrt(x*x+y*y);
f.direction=atan2(y,x);
return f;
}
Force Force::operator-(Force & f1)const
{
Force f;
double x;
double y;
x=abs(magnitude*cos(direction))-abs(f1.magnitude*cos(f1.direction));
y=abs(magnitude*sin(direction))-abs(f1.magnitude*sin(f1.direction));
f.magnitude=sqrt(x*x+y*y);
f.direction=atan2(y,x);
return f;
}
Force Force::operator-()const
{
return Force(magnitude,direction+180);
}
Force Force::operator*(double num)const
{
return Force(num*magnitude,direction);
}
double Force::operator*(Force & f1)const
{
return (magnitude*f1.magnitude*cos(direction-f1.direction));
}
Force Force::operator/(double & num)const
{
return Force(magnitude/num,direction);
}
Force operator*(double & num,Force & f1)
{
return f1*num;
}
std::ostream & operator<<(std::ostream &os,Force &f1)
{
os<<"the force's magnitude is "<<f1.magnitude*angle<<std::endl;
os<<"the force's direction is "<<f1.direction*angle<<std::endl;
}
//main()文件
#include<iostream>
#include"force.h"
using namespace FORCE;
int main()
{
Force f1(10,120),f2(10,120);
Force f3=4*f2;
cout<<f3;
}

