[英]XCode Linker Error, Simple C++ functions
我有一個用xcode編寫的程序,並且只有骨架可以正常運行。 我去添加一些代碼,當我添加三個函數時,它們都是私有的,其中兩個都內嵌到.h和.cpp中。 當我去編譯時,我得到了鏈接器錯誤,因為上帝知道原因。 我正在制作函數的類也繼承自一個結構,但我不認為這應該是一個問題。 病態發布下面的代碼。 (這個項目有很多東西,所以我不能發布所有內容)
#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)
您沒有實現:
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)
就像現在一樣,它們只是在該翻譯單元中定義的自由函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.