(C ++)未定义的类文件错误引用

时间:2016-01-24 04:46:36

标签: c++ function class

当我尝试测试在头文件和实现文件中创建的方法时,我得到以下编译错误:

 undefined reference to 'main_tucker_points::points::points(double, double, double)

我尝试调用的成员函数发生了同样的编译错误。

以下是我的points.h文件的代码:

 namespace main_tucker_points
 {
     class points
     {
     public:
         //CONSTRUCTORS
         points();
         points(double x, double y, double z);

         //MODIFICATION MEMBER FUNCTIONS
         void set_x_point_location(double p_loc);
         void set_y_point_location(double p_loc);
         void set_z_point_location(double p_loc);

         double get_x_coord();
         double get_y_coord();
         double get_z_coord();


         double shift_x_point(double angle, char axis);
         double shift_y_point(double angle, char axis);
         double shift_z_point(double angle, char axis);

     private:
         double x_coord;
         double y_coord;
         double z_coord;
         double x_shift_coord;
         double y_shift_coord;
         double z_shift_coord;
         double x_coord_new;
         double y_coord_new;
         double z_coord_new;
         double angle;
         char axis;
     };
 }
 #endif

这是我的类实现文件的代码,points.cpp。

 #include <cassert>
 #include "points.h"
 using namespace std;

 namespace main_tucker_points
 {
     points::points()
     {

     }

     points::points(double x, double y, double z)
     {
         x_coord = x;
         y_coord = y;
         z_coord = z;       
     }  

     double points::get_x_coord()
     {
         return x_coord;
     }

     double points::get_y_coord()
     {
         return y_coord;
     }

     double points::get_z_coord()
     {
          return z_coord;
     }  
 }

最后,这里是我的main.cpp文件的代码,用于测试成员函数。

 #include <iostream>
 #include <cstdio>
 #include <cstdlib>
 #include "points.h"

 using namespace std;
 using namespace main_tucker_points;

 int main()
 {
     points p1(3.5, 3.6, 3.7);

     printf("Coordinates are %f, %f, %f", p1.get_x_coord(), p1.get_y_coord(), p1.get_z_coord());

     return 0;
 }

1 个答案:

答案 0 :(得分:0)

以这种方式编译

g++ -o myfile points.cpp main.cpp
./myfile

确保#include "points.h"个文件中都有.cpp