简体   繁体   中英

XCode Linker Error, Simple C++ functions

I have a program in xcode and had the thing running fine with just the skeleton. I went to add some code in and when I added three functions, all private, two of which are inline to both the .h and .cpp. When I went to compile I got linker errors for god knows what reason. The class I am making functions in also inherits from a struct but i dont think that should be a problem. Ill post the code below. (there's a lot to this project so i cant post everything)

#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);
}


}

and here is the error its giving me:

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)

You're not implementing:

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

You forgot to qualify these methods in the implementation file.

inline unsigned int l(int i)

is not the same as

inline unsigned int HeapSort::l(int i)

As they are now, they're just free functions defined in that translation unit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM