标题:哪位大哥帮偶看看哪里错了
只看楼主
leileimao
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2005-12-3
 问题点数:0 回复次数:3 
哪位大哥帮偶看看哪里错了

#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
#ifndef VECTOR_H_
#define VECTOR_H_
namespace VECTOR{
class vector
{
private:
double x;
double y;
double mag;
double ang;
char mode;
//private methods for setting values
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
vector();
vector(double n1,double n2,char form='r');
void set(double n1,double n2,char form='r');
~vector();
double xval() const{return x;}
double yval() const{return y;}
double magval() const{return mag;}
double angval() const{return val;}
void polar_mode();
void rect_mode();
//operator overloading
vector operator +(const vertor &b)const;
vector operator -(const vertor &b)const;
vector operator -()const;
vector operator *(double n)const;
//friends
friend vector operator *(double n,const vertor & a);
friend ostream & operator <<(ostream & os ,const vector &v);
};
}
#endif
namespace VECTOR
{
const double Rad_to_deg=57.2957795130823;
//private methods
//calulates magnitude from x and y
void vector::set_mag(){
mag=sqrt(x*x+y*y);
}
void vector::set_ang(){
if(x==0.0&&y=0.0)
ang=0.0;
else
ang=atan2(y,x);
}
void vector::set_x(){
x=mag*cos(ang);
}
void vector::set_y(){
y=mag*sin(ang);
}
//public methods
vector::vector()
{
x=y=mag=ang=0.0;
mode='r';
}
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/Rad_to_deg;
set_x();
set_y();
}
else {
cout<<"Incorrect 3rd argument to vector()--";
cout<<"vector set to 0"<<endl;
x=y=mag=ang=0.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/Rad_to_deg;
set_x();
set_y();
}
vector::~vector()
{
}
void vector::rect_mode()
{
return mode='r';
}
void vector::polar_mode()
{
return mode='p';
}
vector vector::operator +(const vector &b)const
{
return vector(x+b.x,y+b.y);
}
vector vector::operator -(const vector &b)const
{
return vector(x-b.x,y-b.y);
}
vector vector::operator -()const{
return vector(-x,-y);
}
vector::operator *(double n,const vector & a)
{
return a*n;
}
ostream& operator <<(ostream &os ,const vector& v){
if(v.mode=='r'){
os<<"(x,y)=("<<v.x<<","<<v.y<<")";
}
else if(v.mode=='p'){
os<<"(m,a)=("<<v.mag<<","<<v,ang*Rad_to_deg<<")";}
else
os<<"vector object mode is invalid";
return os;
}
}
int main(){
using VECTOR::vector;
srand(time(0));
double direction;
vector step;
vector result(0.0,0.0);
unsigned long steps=0;
double target;
double dstep;
cout<<"Enter target distance(q to quit):";
while (cin>>dstep){
cout<<"Enter step length:";
if(!(cin>>dstep))
break;
while(result.magval()<target)
{
direction=rand()%360;
step.set(dstep,direction,'p');
result=result+step;
steps++;
}
cout<<"After"<<steps<<"steps,the subject ""has the following location:\n";
cout<<result<<endl;
result.polar_mode();
cout<<"or"<<endl<<result<<endl;
cout<<"Average outward distance per step="
<<result.magval()/steps<<endl;
steps=0;
result.set(0.0,0.0);
cout<<"Enter target distance(q to quit):";
}
cout<<"bye"<<endl;
return 0;
}




错误::::
--------------------Configuration: asd - Win32 Debug--------------------
Compiling...
asd.cpp
C:\Documents and Settings\计算机中心\桌面\asd.cpp(33) : error C2146: syntax error : missing ',' before identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(33) : error C2061: syntax error : identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(34) : error C2146: syntax error : missing ',' before identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(34) : error C2061: syntax error : identifier 'b'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(38) : error C2143: syntax error : missing ',' before '&'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(38) : error C2059: syntax error : '&'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(38) : error C2803: 'operator *' must have at least one formal parameter of class type
C:\Documents and Settings\计算机中心\桌面\asd.cpp(52) : error C2106: '=' : left operand must be l-value
C:\Documents and Settings\计算机中心\桌面\asd.cpp(107) : error C2143: syntax error : missing ';' before '{'
C:\Documents and Settings\计算机中心\桌面\asd.cpp(110) : error C2601: 'rect_mode' : local function definitions are illegal
C:\Documents and Settings\计算机中心\桌面\asd.cpp(114) : error C2601: 'polar_mode' : local function definitions are illegal
C:\Documents and Settings\计算机中心\桌面\asd.cpp(118) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.

asd.obj - 12 error(s), 0 warning(s)

搜索更多相关主题的帖子: private include methods setting public 
2005-12-19 11:57
MichaelRain
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2005-12-19
得分:0 
你的程序是干什么的?
说说功能才好改嘛

2005-12-19 12:55
leileimao
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2005-12-3
得分:0 

偶的程序是用来表示矢量的

2005-12-19 21:24
leileimao
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2005-12-3
得分:0 
为什么没有人看啊
2005-12-24 12:02



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-38829-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057255 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved