2012-03-05 54 views
0

我在xcode中有一個程序,並且只有骨架運行正常。我添加了一些代碼,當我添加了三個函數時,都是私有的,其中兩個函數內聯到.h和.cpp。當我去編譯時,我得到了鏈接器錯誤,因爲上帝知道什麼原因。我在做函數的類也從一個結構繼承,但我不認爲這應該是一個問題。我張貼下面的代碼。 (有很多對這個項目,所以我不能完成發佈)XCode鏈接器錯誤,簡單的C++函數

#ifndef HEAP_SORT_H 
#define HEAP_SORT_H 

#include "Interfaces02.h" 
#include "CountedInteger.h" 

class HeapSort : public IHeapSort { 
public: 
HeapSort(); 
virtual ~HeapSort(); 
virtual void buildHeap(std::vector<CountedInteger>& vector); 
virtual void sortHeap(std::vector<CountedInteger>& vector); 
private: 
virtual unsigned int l(int i); 
virtual unsigned int r(int i); 
virtual void fixDown(std::vector<CountedInteger>& vector, int p); 
}; 

#endif 


#include "HeapSort.h" 
#include "CountedInteger.h" 

HeapSort::HeapSort() 
{ 
} 

HeapSort::~HeapSort() 
{ 
} 

void HeapSort::buildHeap(std::vector<CountedInteger>& vector) 
{  

int i = ((int) vector.size()) - 1; 
for(; i > 1; i--) 
{ 
    fixDown(vector, i); 
} 


} 

void HeapSort::sortHeap(std::vector<CountedInteger>& vector) 
{ 
} 

inline unsigned int l(int i) 
{ 
return ((i*2)+1); 
} 

inline unsigned int r(int i) 
{ 
    return ((i*2)+2); 
} 

void fixDown(std::vector<CountedInteger>& vector, int p) 
{ 

int largest; 

if(l(p) <= vector.size() && vector[l(p)] > vector[p]) 
    { 
     largest = l(p); 
    } 
    else 
    { 
     largest = p; 
    } 
if(r(p) <= vector.size() && vector[r(p)] > vector[p]) 
    { 
     largest = r(p); 
    } 
if(largest != p) 
{ 
    CountedInteger temp = vector[largest]; 
    vector[largest] = vector[p]; 
    vector[p] = temp; 
    fixDown(vector, largest); 
} 


} 

,這裏是它給我的錯誤:

Undefined symbols for architecture x86_64: 
"HeapSort::l(int)", referenced from: 
vtable for HeapSort in HeapSort.o 
    "HeapSort::r(int)", referenced from: 
    vtable for HeapSort in HeapSort.o 
    "HeapSort::fixDown(std::vector<CountedInteger,std::allocator<CountedInteger>>&,int)", 
referenced from: 
    vtable for HeapSort in HeapSort.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
+0

您尚未發佈錯誤。 – 2012-03-05 21:06:46

+0

對不起,忘了剛發佈他們 – Dreken105 2012-03-05 21:07:26

+0

沒關係,發現錯誤無論:P – 2012-03-05 21:09:16

回答

3

你不執行:

virtual unsigned int l(int i); 
virtual unsigned int r(int i); 
virtual void fixDown(std::vector<CountedInteger>& vector, int p); 

你忘了在執行文件中限定這些方法。

inline unsigned int l(int i) 

是不一樣的

inline unsigned int HeapSort::l(int i) 

像現在這樣,他們在轉換單元中定義只是免費的功能。

+0

哦,卡車,謝謝我現在感到愚蠢lol – Dreken105 2012-03-05 21:09:43

+0

@ Dreken105發生在每個人身上。 – 2012-03-05 21:10:28

+0

是的每一個其他功能是用骨架預先寫好的,所以當我不得不開始寫我自己的時候,我沒有想過兩次 – Dreken105 2012-03-06 00:39:06