注册 登录
编程论坛 C语言论坛

当没加#ifndef_POINT_H #define_POINT_H #endif 时,项目可以运行,加了后就显示无效预处理。不懂,求助大神

Arther0919 发布于 2019-09-11 16:00, 2402 次点击
//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;
}
4 回复
#2
rjsp2019-09-11 16:09
#ifndef_POINT_H
#define_POINT_H
改为
#ifndef _POINT_H
#define _POINT_H

#3
Arther09192019-09-11 16:29
回复 2楼 rjsp
没用哦
#4
Arther09192019-09-11 16:31
回复 楼主 Arther0919
没加之前好好的,能编译。一加上就报错无效预处理!
#5
jklqwe1112019-09-11 23:38
仔细,仔细,再仔细 报错无效预处理,只有可能是
#ifndef_POINT_H
#define_POINT_H
#endif
#include <iostream>
#include <cmath>
#include "point.h"
这几句中有错误,不会有其他原因。
1