Undefined symbols of the architechture x86_64 [closed]
ok I am learning c++ and I got this error
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
here is my code
#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;
}
any help is appreciated!
You only declared the functions:
void set(int new_x, int new_y);
Point add (const Point &pt);
But You did not provide definitions for them. So the linker cannot find their definitions and complains about it, The compiler tells you to provide a definition for those two functions, and you should.
An empty(which does nothing) definition of the functions look like:
void set(int new_x, int new_y)
{
}
Point add (const Point &pt)
{
Point temp;
return temp;
}
Disclaimer: You should replace these definitions with your actual implementation, The above shall just let you compile and link sucessfully(not work as you want to)
精彩评论