2014-04-20 79 views
0

我試圖修復返回空向量的函數。我已經做了一些谷歌搜索,這也表明我需要一個拷貝構造函數的類Point,我不斷收到此錯誤:體系結構x86_64的未定義符號(複製構造函數)

Undefined symbols for architecture x86_64: "Point::Point(Point const&)", referenced from: 
     Core::render() in Week3_T.o 
     Core::sortTriVertices(Point&, Point&, Point&) in Week3_T.o 
     Core::decompose(std::__1::vector<Point, std::__1::allocator<Point> >) in Week3_T.o 
     Core::drawTriangle(Point, Point, Point) in Week3_T.o 
     Core::clipedge(std::__1::vector<Point, std::__1::allocator<Point> >, int, int, int, int) in Week3_T.o 
     std::__1::enable_if<__is_forward_iterator<Point*>::value, void>::type std::__1::vector<Point, std::__1::allocator<Point> 
>::__construct_at_end<Point*>(Point*, Point*) in Week3_T.o 
     void std::__1::vector<Point, std::__1::allocator<Point> >::__push_back_slow_path<Point const>(Point const&) in Week3_T.o 
     ... ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 

和我的課:

class Point 
{ 
public: 
    int x; 
    int y; 
    Uint8 r; 
    Uint8 g; 
    Uint8 b; 
    Point(int x, int y, Uint8 r, Uint8 g, Uint8 b) : x(x), y(y), r(r), g(g), b(b) {} 
    Point& operator=(Point const &np){ 
     x=np.x; 
     y=np.y; 
     r=np.r; 
     g=np.g; 
     b=np.b; 
     return *this; 
    } 
    Point(const Point&); 
    Point(){} 


}; 

和代碼片段:

std::vector<Point> Core::clipedge(vector<Point> polygon, int x0, int y0, int x1, int y1) 
{ 

    int r=rand()%255; 
    int g=rand()%255; 
    int b=rand()%255; 
    // For each side of the polygon, check the 4 cases of sutherland-hodgman. 
    // Creating a temporary buffer to hold your new set of vertices for the clipped polygon. 
    std::vector<Point> temp; 
    temp.push_back(Point(1,2,255,255,255)); 
    int size = (int)polygon.size(); 

    for (int i = 0; i < size; ++i) { 
     {....} 
     //push_back clipped point 
     temp.push_back(p1); 
     {....} 
    } 
    return temp; 

系統OSX10.9.2和IDE是xcode5.1.1編譯器是蘋果llvm5.1

我需要做些什麼來解決這個問題?感謝幫助。

回答

2

正如在其他答案中已經指出的那樣,您沒有提供Point::Point(const Point&)的定義,但是您已經在類定義中聲明瞭它的定義。但是你的班級不需要特殊的複製和分配處理,所以你的問題的解決方案是而不是提供缺少的定義,但刪除複製構造函數的聲明。取出賦值運算符太:

class Point 
{ 
public: 
    int x; 
    int y; 
    Uint8 r; 
    Uint8 g; 
    Uint8 b; 
    Point(int x, int y, Uint8 r, Uint8 g, Uint8 b) 
    : x(x), y(y), r(r), g(g), b(b) {} 
}; 

編譯器合成的版本將做正確的事情。

+0

感謝您的幫助,如何確定是否需要複製構造函數? – user3508896

+0

@ user3508896如果您需要進行除複製(或分配)數據成員以外的任何操作,您確實需要它們。 – juanchopanza

0

很可能是你沒有定義你的* .C文件Point(const Point&); .... 它編譯,因爲你已經在* .h文件中聲明,但未能在試圖因爲符號鏈接代碼的其他部分指的是在對象文件或庫中找不到的。

+0

OMG,我確定它在.c文件中,只是意外刪除。非常感謝。 – user3508896

+0

很奇怪,我的函數還在返回空向量,我會發表另一個問題。請幫助,非常感謝。 – user3508896

相關問題