令人困惑的内联函数
为什么Vector() 和 set(double n1, double n2, char form = 'r')不能设为内联函数:
//vect.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iostream>
class Vector {
private:
double x;
double y;
char mode;
public:
Vector();
Vector(double n1,double n2,char form = 'r') {x = n1; y = n2;mode=form;}
void set(double n1,double n2,char form = 'r');
~Vector() {}
double xval()const { return x; }
double yval()const { return y; }
double magval()const;
double angval()const;
void polar_mode();
void vect_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.cc
#include <cmath>
#include "vect1.h"
const double Rad_to_deg = 57.2957795130823;
Vector::Vector() {
x = y = 0.0;
mode = 'r';
}
void Vector::set(double n1, double n2, char form) {
x = n1;
y = n2;
mode = form;
}
double Vector::magval()const {
return sqrt(x*x + y*y);
}
double Vector::angval()const {
if (x == 0.0 && y == 0.0)
return 0;
else
return atan2(y, x);
}
void Vector::polar_mode() {
double temp;
temp = sqrt(x*x + y*y);
y = angval();
x = temp;
mode = 'p';
}
void Vector::vect_mode() {
double temp;
temp = x * cos(y);
y = x * sin(y);
x = temp;
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 Vector(n*a.x, n*a.y);
}
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.x << ", " << v.y * Rad_to_deg << ") ";
else
os << "Vector object mode is invalid";
return os;
}
//randwalk.cc
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include "vect.h"
const int SIZE = 60;
int main(void)
{
srand(time(0));
char filename[SIZE];
std::ofstream fout;
Vector step;
Vector result(0.0, 0.0);
unsigned long steps = 0;
double direction;
double dstep;
double target;
std::cout << "Enter data file's name: ";
std::cin.getline(filename, SIZE);
fout.open(filename);
if (!fout.is_open()) {
std::cout << "Can't open the file " << filename << std::endl
<< "Program terminating.\n";
exit(EXIT_FAILURE);
}
std::cout << "Target distance(q to quit): ";
while (std::cin >> target) {
std::cout << "step length: ";
if (!(std::cin >> dstep))
break;
while (result.magval() < target) {
direction = rand() % 360;
step.set(dstep, direction, 'p');
step.vect_mode();
result = result + step;
fout << steps++ << ": " << result << std::endl;
}
fout << "After " << steps << " steps, the subject "
"has the following location:\n" << result
<< std::endl << "or\n";
result.polar_mode();
fout << result << std::endl;
fout << "Average outward distance per step = "
<< result.magval() / steps << std::endl;
steps = 0;
result.set(0.0, 0.0);
std::cout << "target distance(q to quit): ";
}
fout << "bye!\n";
fout.close();
return 0;
}