求达人告诉我这个错误是怎么回事。
head.h文件
- C/C++ code
#ifndef PERSON_H_00#define PERSON_H_00#include <string>#include <iostream>using namespace std;class Vector{private : double x,y,mag,ang; char mode ; void set_x(); void set_y(); void set_mag(); void set_ang();public : Vector(){ x = y = mag = ang = 0; mode = 'r';} Vector(double n1, double n2, char form = 'r'); void set(double n1, double n2, char form = 'r'); double xval(){ return x; } double yval(){ return y; } double magval(){return mag;} double angval(){return ang;} void polar_mode() { mode = 'p'; } void rect_mode(){ mode = 'r'; } Vector operator+(const Vector &V)const; Vector operator-(const Vector &V)const; Vector operator-()const; Vector operator*(double n)const; friend Vector operator * (double n, const Vector &V); friend ostream operator << (ostream &coutt, const Vector &V);};#endifhead.cpp文件
- C/C++ code
#include "stdafx.h"#include <iostream>#include <string>#include <cmath>#include "head.h"using namespace std;const double VAL = 57.29577951308;void Vector::set_x(){ x = mag*cos(ang); }void Vector::set_y(){ y = mag*sin(ang); }void Vector::set_mag(){ mag = sqrt(pow(x,2)+pow(y,2)); }void Vector::set_ang(){ if(x == 0&& y==0) mag = ang = 0; else ang = atan2(y,x);}Vector::Vector(double n1, double n2, char form){ mode = form; if(form == 'r'){ x = n1; y = n2; set_mag(); set_ang(); } else if(form == 'p') { mag = n1; ang = n2/VAL; set_x(); set_y(); } else { cout<<"选择的保存矢量类型有错误!选用默认值进行设置."<<endl; x = y = mag = ang = 0; mode = 'r'; }}void Vector::set(double n1, double n2, char form){ mode = form; if(form == 'r'){ x = n1; y = n2; set_mag(); set_ang(); } else if(form == 'p') { mag = n1; ang = n2/VAL; set_x(); set_y(); } else { cout<<"选择的保存矢量类型有错误!选用默认值进行设置."<<endl; x = y = mag = ang = 0; mode = 'r'; }}Vector Vector::operator+(const Vector &V)const{ return Vector(x+V.x, y+V.y); }Vector Vector::operator-(const Vector &V)const{ return Vector(x - V.x, y - V.y); }Vector Vector::operator * (double n)const{ return Vector(x*n, y*n); }Vector operator * (double n, const Vector &V){ return V*n; }ostream operator << (ostream &coutt, const Vector &V){ if(V.mode == 'r') coutt<<"(x,y) = "<<V.x<<","<<V.y<<")"<<endl; else if(V.mode == 'p') coutt<<"(m,a) = "<<V.mag<<','<<V.ang<<")"<<endl; else coutt<<"矢量的类型有问题!不能输出!"<<endl; return coutt;}test.cpp文件
- C/C++ code
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include "head.h"
#include <string>
#include <cstdlib>
#include <windows.h>
#include <ctime>
using namespace std;
void main()
{
Vector result, temp;
double distance = 0,steplen = 0,direct = 0;
int count = 0;
srand(time(0));
cout < <"请输入离开原点的距离:";
cin>>distance;
cout < <"请输入步长:";
cin>>steplen;
while(result.magval() <=distance)
{
count++;
direct = rand()%360;
temp.set(steplen, distance,'p');
result = result + temp;
}
cout < <"经过" < <count < <"步走出" < <distance < <"米的距离" < <endl;
cout < <"现在的位置:" < <endl;
result.polar_mode();
cout < <result < <endl;
cout < <"或" < <endl;
result.rect_mode();
cout < <result < <endl;
cout < <"现在距离原点" < <result.magval() < <"米" < <endl;
}
-----------------------
显示的错误如下:
1>------ 已启动生成: 项目: test, 配置: Debug Win32 ------
1>正在编译...
1>head.cpp
1>d:\安装文件\visual studio 2008\vc\include\ostream(584) : error C2248: “std::basic_ios<_Elem,_Traits>::basic_ios”: 无法访问 private 成员(在“std::basic_ios<_Elem,_Traits>”类中声明)
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> d:\安装文件\visual studio 2008\vc\include\ios(151) : 参见“std::basic_ios<_Elem,_Traits>::basic_ios”的声明
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> 此诊断出现在编译器生成的函数“std::basic_ostream<_Elem,_Traits>::basic_ostream(const std::basic_ostream<_Elem,_Traits> &)”中
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>正在生成代码...
1>正在编译...
1>test.cpp
1>正在生成代码...
1>生成日志保存在“file://d:\CODE\test\test\Debug\BuildLog.htm”
1>test - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
[解决办法]
可能你的编译器不支持友元对私有成员的访问,换一个编译器试试
我在Code::Blocks测试了下,输出如下:
请输入离开原点的距离:21
请输入步长:4
经过6步走出21米的距离
现在的位置:
(m,a) = (24 , 0.366519)
或
(x,y) = (22.4059 , 8.60083)
现在距离原点24米