架构x86_64的未定义符号

时间:2011-09-10 19:13:10

标签: c++

好的我正在学习c ++而且我收到了这个错误

Undefined symbols for architecture x86_64:
  "Point::set(int, int)", referenced from:
      Point::Point(int, int)in ccHkya9E.o
  "Point::add(Point const&)", referenced from:
      Point::operator+(Point const&)in ccHkya9E.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

这是我的代码

#include<iostream>

 using namespace std;

 class Point {
 private:
   int x, y;
 public:
   Point() {}
   Point(int new_x, int new_y) {set(new_x, new_y);}
   Point (const Point & src) {set(src.x, src.y);}

 //Operations
   Point add (const Point &pt);
   Point sub (const Point &pt);
   Point operator+(const Point &pt) {return add(pt);}
   Point operator-(const Point &pt) {return sub(pt);}
 //other member functions
   void set(int new_x, int new_y);
   int get_x() const {return x;}
   int get_y() const {return y;}
 };

 int main() {
   Point point1(20,20);
   Point point2(0,5);
   Point point3(-10, 25);
   Point point4=point1+point2+point3;

   cout<<"the point is"<<point4.get_x();
   cout<<","<<point4.get_y()<<"."<<endl;
   return 0;
 }

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:7)

您只声明了这些功能:

void set(int new_x, int new_y);
Point add (const Point &pt);

但你没有为他们提供定义。所以链接器找不到它们的定义并抱怨它,编译器告诉你提供这两个函数的定义,你应该这样做。

函数的空(无效)定义如下:

void set(int new_x, int new_y)
{

}
Point add (const Point &pt)
{
    Point temp;
    return temp;
}

免责声明:您应该将这些定义替换为您的实际实现,以上内容只是让您成功编译和链接(不能按您的意愿工作)