当没加#ifndef_POINT_H #define_POINT_H #endif 时,项目可以运行,加了后就显示无效预处理。不懂,求助大神
//point.h#ifndef_POINT_H
#define_POINT_H
class Point{
public:
double getX() const{ //规定为const函数
return x;
};
double getY() const{
return y;
};
Point(double newx = 0, double newy = 0):x(newx) ,y(newy){}
Point(){
x = 0;
y = 0;
}
private:
double x, y;
}; //不要忘记分号
#endif
//.cpp
#include <iostream>
#include <cmath>
#include "point.h"
using namespace std;
double LineFit(const Point points[], int npoint){
double aX = 0, aY = 0;
double lxx = 0, lyy = 0, lxy = 0;
for(int i = 0; i < npoint; i++){
aX += points[i].getX()/npoint;
aY += points[i].getY()/npoint;
}
for(int i = 0; i < npoint; i++){
lxx += (points[i].getX() - aX)*(points[i].getX() - aX);
lyy += (points[i].getY() - aY)*(points[i].getY() - aY);
lxy += (points[i].getY() - aY)*(points[i].getX() - aX);
}
cout << "This line can be fixxed by y=ax+b" << endl;
cout << "a = " << lxy/lyy << endl;
cout << "b = " << aY - lxy*aX/lxx << endl;
return static_cast<float>(lxy/sqrt(lxx*lyy));
}
int main()
{
Point points[3] = {Point(3,3), Point(4,4), Point(5,5)};
float r = LineFit(points, 3);
cout << "Line coefficint r = " << r << endl;
return 0;
}