[英]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.