注册 登录
编程论坛 C++教室

C++头文件与CPP文件所包含的声明有什么区别,为什么这个小程序头文件出问题了

剑飘香 发布于 2011-12-10 23:13, 1391 次点击
//Point.h

#ifndef Point.h
#define Point.h
class Point{
      double x,y;
      public:
             Point(double a=0,double b=0):x(a),y(b){}
             double yuandian()const;
             double liangdian(const Point& obj)const;
             int getx()const;
             int gety()const;
             void setxy(double a,double b);
             void move(double a,double b);
             };
#endif            
      
//Point.cpp


#include "Point.h"  
#include<math.h>
using namespace std;
             void Point::yuandian()const
             {
                  return sqrt(x*x+y*y);
             }
            
             void Point::liangdian(const Point& obj)const
             {
                  return sqrt(((x-obj.x)*(x-obj.x)+((y-obj.y)*(y-obj.y));
             }
            
             int Point::getx()const
             {
                 return x;
             }
             int Point::gety()const
             {
                 return y;
             }
             void Point::setxy(double a,double b)
             {
                  x=a;
                  y=b;
             }
            
             void Point::move(double a,double b)
             {
                  x=a;
                  y=b;
             }

//main

#include <cstdlib>
#include<math.h>
#include <iostream>
#include "Point.h"  
using namespace std;

int main(int argc, char *argv[])
{
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

[ 本帖最后由 剑飘香 于 2011-12-10 23:15 编辑 ]
5 回复
#2
共和国鹰派2011-12-11 00:07
double yuandian()const;

 void Point::yuandian()const

声明和定义不匹配所以错误
#3
lonely_212011-12-11 00:16
#include "tt.h"  
#include<math.h>
#include <iostream>
using namespace std;
double Point::yuandian()const    //返回值有问题
{
    return sqrt(x*x+y*y);
}

double Point::liangdian(const Point& obj)const  //同上
{
    return sqrt((x-obj.x)*(x-obj.x)+((y-obj.y)*(y-obj.y)));
}

int Point::getx()const
{
    return x;
}
int Point::gety()const
{
    return y;
}
void Point::setxy(double a,double b)
{
    x=a;
    y=b;
}

void Point::move(double a,double b)
{
    x=a;
    y=b;
}
   



#ifndef Point_
#define Point_
class Point{
double x,y;
public:
             Point(double a=0.0,double b=0.0):x(a),y(b){}
             double yuandian()const ;
             double liangdian(const Point& obj)const;
             int getx()const;
             int gety()const;
             void setxy(double a,double b);
             void move(double a,double b);
};
#endif            
  
看一下,应该明白为什么错了吧
#4
剑飘香2011-12-11 10:11
回复 2楼 共和国鹰派
改过了可是还有问题,无法编译,显示头文件编译不通过
#5
lonely_212011-12-11 21:48
回复 3楼 lonely_21
好着呀,我试了一下我改的可以通过呀
#6
lonely_212011-12-11 21:52
#ifndef Point_ ////这个也改了,你再试试
#define Point_////
class Point{
double x,y;
public:
             Point(double a=0.0,double b=0.0):x(a),y(b){}
             double yuandian()const ;
             double liangdian(const Point& obj)const;
             int getx()const;
             int gety()const;
             void setxy(double a,double b);
             void move(double a,double b);
};
#endif            
1