c++为什么类的定义和实现分别装在头文件和源文件里时提示找不到成员函数的定义 5
申明---------------------------------------------------#ifndefPOINT_H_INCLUDED#definePO...
申明---------------------------------------------------
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
class Point
{
int x,y;
public:
Point();
Point(int xx, int yy);
Point (Point &p);
void set(int xx, int yy)
{ x=xx;y=yy;}
int GetX() const
{ return x;}
int GetY() const
{ return y;}
};
#endif // POINT_H_INCLUDED
申明结束-----------------------------------------------------------------
定义--------------------------------------------------------
/* Header file “point.cpp” */
#include<iostream>
#include"point.h"
using namespace std;
Point::Point()
{ x=0;y=0;}
Point::Point(int xx, int yy)
{ x=xx;y=yy;}
Point::Point(Point &p)
{ x=p.x;y=p.y;}
定义结束-------------------------------------------------
主程序----------------------------------------------------------
#include<iostream>
using namespace std;
#include "point.h"
int main()
{ Point myp1(1,1), myp2;
return 0;
}
主程序结束-------------------------------------------------------
编译时就会报错
undefined reference to `Point::Point(int, int)'
undefined reference to `Point::Point()'|
但是如果把函数定义加在头文件的下面就可以运行 展开
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
class Point
{
int x,y;
public:
Point();
Point(int xx, int yy);
Point (Point &p);
void set(int xx, int yy)
{ x=xx;y=yy;}
int GetX() const
{ return x;}
int GetY() const
{ return y;}
};
#endif // POINT_H_INCLUDED
申明结束-----------------------------------------------------------------
定义--------------------------------------------------------
/* Header file “point.cpp” */
#include<iostream>
#include"point.h"
using namespace std;
Point::Point()
{ x=0;y=0;}
Point::Point(int xx, int yy)
{ x=xx;y=yy;}
Point::Point(Point &p)
{ x=p.x;y=p.y;}
定义结束-------------------------------------------------
主程序----------------------------------------------------------
#include<iostream>
using namespace std;
#include "point.h"
int main()
{ Point myp1(1,1), myp2;
return 0;
}
主程序结束-------------------------------------------------------
编译时就会报错
undefined reference to `Point::Point(int, int)'
undefined reference to `Point::Point()'|
但是如果把函数定义加在头文件的下面就可以运行 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询