调了两小时友元函数过不了,重新建一个项目马上就行了
VS真坑!
vector112.h
#ifndef VECTOR112_H_ #define VECTOR112_H_ #include <iostream> namespace VECTOR112 { //class Vector112; //std::ostream&operator<<(std::ostream &os,const Vector112 &v); class Vector112{ public: enum Mode{RECT,POL}; private: double x; double y; Mode mode; //void set_mag(); //void set_ang(); void set_x(double mag,double ang);//改成接收一个mag,然后设置x void set_y(double mag,double ang); public: Vector112(); Vector112(double n1,double n2,Mode form=RECT); void reset(double n1,double n2,Mode form=RECT); ~Vector112(); double xval()const {return x;}; double yval()const{return y;}; double magval()const;//这里开始改了 double angval()const;//必须用const限定,否则重载<<会出错 void polar_mode(); void rect_mode(); Vector112 operator+(const Vector112 &b)const; Vector112 operator-(const Vector112 &b)const; Vector112 operator-()const; Vector112 operator*(double n)const; friend Vector112 operator*(double n,const Vector112 &v); //friend std::ostream&operator<<(std::ostream &os,const Vector112 &v); friend std::ostream & operator<<(std::ostream &os,const Vector112 &v); }; }; #endif
vector112.cpp
#include "vector112.h" #include <cmath> //using namespace std; //using namespace VECTOR112; using std::sqrt; using std::sin; using std::cos; using std::atan; using std::atan2; using std::cout; namespace VECTOR112{ const double Rad_to_deg=45.0/atan(1.0); void Vector112::set_x(double mag,double ang)//改成接收mag,ang { x=mag*cos(ang);//设置x } void Vector112::set_y(double mag,double ang) { y=mag*sin(ang); } Vector112::Vector112() { x=y=0.0; mode=RECT; } Vector112::Vector112(double n1,double n2,Mode form) { mode=form; if(form==RECT) { x=n1; y=n2; } else if(form==POL) { set_x(n1,n2); set_y(n1,n2); } else { cout<<"Incorrect 3rd argument to Vector()--"; cout<<"vector set to 0\n"; x=y=0.0; mode=RECT; } } void Vector112::reset(double n1,double n2,Mode form)//重置值 { mode=form; if(form==RECT) { x=n1; y=n2; } else if(form==POL) { set_x(n1,n2); set_y(n1,n2); } else { cout<<"Incorrect 3rd argument to Vector()--"; cout<<"vector set to 0\n"; x=y=0.0; mode=RECT; } } double Vector112::magval()const { return sqrt(x*x+y*y);//计算公式看书本的set_mag } double Vector112::angval()const//计算公式看书本的set_ang { if(x==0.0&&y==0.0) return 0.0; else return atan2(y,x); } void Vector112::polar_mode() { mode=POL;//直接将状态改了??? } void Vector112::rect_mode() { mode=RECT;//x,y不变的啊! } Vector112::~Vector112() {}; Vector112 Vector112::operator+(const Vector112 &v)const { return Vector112(x+v.x,y+v.y); } Vector112 Vector112::operator-(const Vector112 &v)const { return Vector112(x-v.x,y-v.y); } Vector112 Vector112::operator-()const { return Vector112(-x,-y); } Vector112 Vector112::operator*(double n)const { return Vector112(x*n,y*n); } //friend methods Vector112 operator*(double n,const Vector112 &v) { return v*n; } //不知为何,在上一个项目里面编译这个友元函数总是报错 //说无法访问v.mode中的mode,以及v.x中的x //可能是上个项目太多东西了,obj有点混 std::ostream & operator<<(std::ostream &os,const Vector112 &v) { if(v.mode==Vector112::RECT)//必须用Vector:: os<<"(x,y)=("<<v.x<<","<<v.y<<")"; else if(v.mode==Vector112::POL) { os<<"(m,a)=("<<v.magval()<<","<<v.angval()*Rad_to_deg<<")"; } else os<<"Vector object mode is invalid!"; return os; } };
main.cpp
#include <iostream> #include <fstream> #include <cstdio> #include <ctime> //#include <vector> #include "vector112.h" using namespace std; using namespace VECTOR112; int main() { ofstream fin; //fin.open("111.txt");//打开文件 //if(fin.fail()) //cout<<"open fali!"<<endl; //else //cout<<"Sucess!"<<endl; //fin<<"111"; srand(time(0)); double direction; Vector112 step; Vector112 result(0.0,0.0); unsigned long steps=0; double target;//要走的距离 double dstep;//每一步的距离 cout<<"Enter step length:(q to quit:)"; while(cin>>target) { cout<<"Enter step length:"; if(!(cin>>dstep)) break; int i=0; while(result.magval()<target) { //fin<<i<<" (x,y)= "<<"(";//1 //fin<<"the "<<i++<<" input is"<<endl;//test //fin<<"\n"<<i++<<" "; //result.show(fin);//2 //fin<<" "<<endl;//1+2+3 direction=rand()%360; step.reset(dstep,direction,Vector112::POL); result=result+step; steps++; } cout<<"After "<<steps<<" steps ,the subject " <<" has the following location:"<<endl; //fin<<"After "<<steps<<" steps ,the subject " //<<" has the following location:"<<endl; // fin<<result; result.polar_mode(); //cout<<" or \n"<<result<<endl; //fin<<" or \n"; //fin<<result<<endl; cout<<"Average outward distance per step=" <<result.magval()/steps<<endl; //fin<<"Average outward distance per step=" //<<result.magval()/steps<<endl; steps=0; result.reset(0.0,0.0); cout<<"Enter target distance (q to quit):"; } //fin<<"Bye!\n"; cin.clear(); while(cin.get()!='\n') continue; //cin.get(); fin.close(); cin.get(); return 0; }
不要喋喋不休地问为什么,因为有时候并不是弄错了,编译器也是个坑!
——————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:https://www.royalchen.com/
author:royalchen
Email:royalchen@royalchen.com
———————————————————————————————————————————————————