书上的vector例程,编译不过.费解,请教..谢谢!

taurusbbstech 2008-04-07 10:50:38
//vect.h -- Vector class with << ,mode state
#ifdef VECTOR_H_
#define VECTOR_H_
#include <iostream>
namespace VECTOR
{
class Vector
{
private:
double x;
double y;
double mag;
double ang;
char mode;

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 ang;}
void polar_mode();
void rect_mode();

Vector operator+(const Vector & b)const;
Vector operator-(const Vector & b)const;
Vector operator-()const;
Vector operator*(double n)const;

friend Vector operator*(double n, const Vector & a);
friend std::ostream & operator<<(std::ostream & os, const Vector & v);
};
}
#endif
//vect.cpp -- methods for Vector class
#include <cmath>
#include <iostream>
#include "vector.h"
//using std::sin;
//using std::cos;
//using std::atan2;
using std::cout;

namespace VECTOR
{
const double Rad_to_deg = 57.2957795130823;
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);
}

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\n";
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;
set_x();
set_y();
}
else
{
cout << "Incorrect 3rd argument to Vector()-- ";
cout << "vector set to 0\n";
x = y = mag = ang = 0.0;
mode = 'r';
}
}

Vector::~Vector()
{
}

void Vector::polar_mode()
{
mode = 'p';
}

void Vector::rect_mode()
{
mode = 'r';
}

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 Vector::operator*(double n)const
{
return Vector(n * x, n * y);
}

Vector operator*(double n, const Vector & a)
{
return a * n;
}

std::ostream & operator<<(std::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;
}
}
//randwalk.cpp -- using the Vector class

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"

int main()
{
using namespace std;
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 >> target)
{
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\n" << 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!\n";
return 0;
}
...全文
174 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
AwL_1124 2011-09-14
  • 打赏
  • 举报
回复
修改vector.cpp、randwalk.cpp中#include内容为: "vect.h"
AwL_1124 2011-09-14
  • 打赏
  • 举报
回复
求解决方法
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
还有被我注释了的三条
//using std::sin;
//using std::cos;
//using std::atan2;
书上确是这样用的.
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 Chiyer 的回复:]
你先去google一下 #ifdef #ifndef 的作用吧
[/Quote]
我知道,是打错了.
星羽 2008-04-07
  • 打赏
  • 举报
回复
你先去google一下 #ifdef #ifndef 的作用吧
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
呵呵..粗心了,谢谢大家.
taodm 2008-04-07
  • 打赏
  • 举报
回复
哈哈,如果你书上真是#ifdef VECTOR_H
你还是把这书扔了吧。
iambic 2008-04-07
  • 打赏
  • 举报
回复
因为你没定义VECTOR_H,所以后面的东西都被预处理去掉了。
#ifdef VECTOR_H
//everything...
#endif
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 liyuzhu_1984 的回复:]
那本书上的? 你的程序引用头文件可能有错误
[/Quote]
C++ primer plus.
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 taodm 的回复:]
不要叫vector.h。重名了。
[/Quote]
改成vect.h,还是一样的错.
liyuzhu_1984 2008-04-07
  • 打赏
  • 举报
回复
那本书上的? 你的程序引用头文件可能有错误
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yinyuexiuluo 的回复:]
当然通不过了,你的vector头文件就错了,
#ifndef VECTOR_H
#define VECTOR_H
[/Quote]
为什么这样是错的?
taodm 2008-04-07
  • 打赏
  • 举报
回复
不要叫vector.h。重名了。
hellodudu 2008-04-07
  • 打赏
  • 举报
回复
hehe up ls
lz粗心了
taurusbbstech 2008-04-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 taodm 的回复:]
啥编译器,啥错误?
兄弟,要节约别人的时间,才能有好的回报。
[/Quote]

VS6

Deleting intermediate files and output files for project 'vector - Win32 Debug'.
--------------------Configuration: vector - Win32 Debug--------------------
Compiling...
randwalk.cpp
\vector\randwalk.cpp(11) : error C2653: 'VECTOR' : is not a class or namespace name
\vector\randwalk.cpp(11) : error C2873: 'Vector' : symbol cannot be used in a using-declaration
\vector\randwalk.cpp(14) : error C2065: 'Vector' : undeclared identifier
\vector\randwalk.cpp(14) : error C2146: syntax error : missing ';' before identifier 'step'
\vector\randwalk.cpp(14) : error C2065: 'step' : undeclared identifier
\vector\randwalk.cpp(15) : error C2146: syntax error : missing ';' before identifier 'result'
\vector\randwalk.cpp(15) : error C2065: 'result' : undeclared identifier
\vector\randwalk.cpp(25) : error C2228: left of '.magval' must have class/struct/union type
\vector\randwalk.cpp(25) : fatal error C1903: unable to recover from previous error(s); stopping compilation
vector.cpp
\vector\vector.cpp(13) : error C2653: 'Vector' : is not a class or namespace name
\vector\vector.cpp(15) : error C2065: 'mag' : undeclared identifier
\vector\vector.cpp(15) : error C2065: 'x' : undeclared identifier
\vector\vector.cpp(15) : error C2065: 'y' : undeclared identifier
\vector\vector.cpp(15) : warning C4552: '-' : operator has no effect; expected operator with side-effect
\vector\vector.cpp(18) : error C2653: 'Vector' : is not a class or namespace name
\vector\vector.cpp(21) : error C2065: 'ang' : undeclared identifier
\vector\vector.cpp(24) : error C2143: syntax error : missing ';' before '}'
\vector\vector.cpp(26) : error C2653: 'Vector' : is not a class or namespace name
\vector\vector.cpp(31) : error C2653: 'Vector' : is not a class or namespace name
\vector\vector.cpp(36) : error C2653: 'Vector' : is not a class or namespace name
\vector\vector.cpp(39) : error C2065: 'mode' : undeclared identifier
\vector\vector.cpp(40) : warning C4508: 'Vector' : function should return a value; 'void' return type assumed
\vector\vector.cpp(42) : error C2653: 'Vector' : is not a class or namespace name
\vector\vector.cpp(66) : warning C4508: 'Vector' : function should return a value; 'void' return type assumed
\vector\vector.cpp(69) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
Creating browse info file...
BSCMAKE: error BK1506 : cannot open file '.\Debug\randwalk.sbr': No such file or directory
Error executing bscmake.exe.

vector.exe - 23 error(s), 3 warning(s)
yinyuexiuluo 2008-04-07
  • 打赏
  • 举报
回复
当然通不过了,你的vector头文件就错了,
#ifndef VECTOR_H
#define VECTOR_H
taodm 2008-04-07
  • 打赏
  • 举报
回复
啥编译器,啥错误?
兄弟,要节约别人的时间,才能有好的回报。

64,849

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧