c++对对象的未定义引用
C++ undefined reference to object
我正在做一个简单的c++项目来实现堆类并使用它的函数。我已经写出了大部分代码,当我去尝试一些函数时,当我试图编译时,控制台会告诉我有未引用的符号。代码如下:
Heap.h:
#ifndef HEAP_H
#define HEAP_H
template<typename T>
class Heap {
T arr[100];
int heapSize;
//int parent(int);//returnts the index of the parent
int leftChild(int);//returns the index of the left child
int rightChild(int);//returns the index of the right child
void heapify(T*,int);//made to build the heap;
void buildHeap();//constructs the heap
public:
Heap();//default constructor
Heap(T[], int);//custom constructer accepts an array of type T and size
~Heap();//destructor
void insert(T);//insert a node into the array
// void heapSort(T*);
T extractMax (); //return root value and remove it from heap
T maximum();//return the maximum
void printHeap();//print the values of the heap
};
#endif
Heap.cpp:
#include "Heap.h"
#include <iostream>
template<typename T>
Heap<T>::Heap() { heapSize = 0; }
template<typename T>
Heap<T>::Heap(T a[], int size) {//accepts an array of Type T
for(int i =0; i < size; i++)
arr[i] = a[i];
heapSize = size;
buildHeap();
}
template<typename T>
Heap<T>::~Heap() { }
template<typename T>
int Heap<T>::leftChild(int i) { return ((i*2) + 1); } //the left child is given by index*2 +1
template<typename T>
int Heap<T>::rightChild(int i) { return ((i*2) + 2); } //the right child is given by index*2 +2
template<typename T>
void Heap<T>::heapify(T* a, int i )
{
int left, right, largest;
T temp;
left = leftChild(i);//get the index for the left child
right = rightChild(i);//get the index for the right child
if((left <= heapSize) && a[left] > a[i])//check which index has the largest value and store it
//also checks if the index is within the heap size
largest = left;
else
largest = i;
if((right <= heapSize) && a[right] > a[largest])//check if right child is larger
largest = right;
if(largest != i)//swap the values of the child is larger than parent
{
temp = a[i];
a[i] = a[largest];
a[largest] = temp;
heapify(a, largest);
}
}
template<typename T>
void Heap<T>::buildHeap()
{
int hSize = heapSize;//takes the size of the heap
for(int i = ((hSize/2)-1); i >= 0 ; i--)//loops through every node to make sure they are sorted
heapify(arr, i);
}
template<typename T>
T Heap<T>::maximum() { return arr[0]; } //return the first element or root element;
template<typename T>
T Heap<T>::extractMax() //removes the root element and returns it
{
T max = arr[0];
T* temp = new T[heapSize-1];//create a temp array
for(int i = 0; i < heapSize; i++)//store all old vals to temp arr except for first element;
temp[i] = arr[i+1];
arr = temp;//set arr to temp array
heapSize--;//decerease heapsize
buildHeap(); //rebuild heap
return max; //return root value;
}
template<typename T>
void Heap<T>::insert(T add) //add is the new element for the heap
{
arr[heapSize] = add;//add the new element to the end of the arr
heapSize++;//increase heapsize
buildHeap(arr, heapSize);//call buildHeap to rebuild and resort the heap with the new element
}
template<typename T>
void Heap<T>::printHeap()
{
for(int i =0; i < heapSize; i++)
std::cout<<arr[i];
}
Main.cpp:
#include "Heap.h"
#include <iostream>
using namespace std;
int main()
{
int arr[] = {6,7,9,10,2,4,5};
Heap<int> heap(arr, 7);
heap.printHeap();
return 0;
}
我的makefile。
all: heapmake
heapmake: Main.o Heap.o
g++ Main.o Heap.o -o heapmake
Main.o:Main.cpp
g++ -c Main.cpp
Heap.o:Heap.cpp
g++ -c Heap.cpp
clean:
rm *o heapmake
我怀疑我没有正确地将文件链接在一起,但我想我会发布所有的代码,以防万一。
模板需要在实例化时使它们的定义可见,因此将它们放在.cpp
文件中(几乎)是不可能的。
将定义移动到标题(可以是Heap.h
,也可以是新的标题,例如HeapImpl.h
)。您也可以将#include "Heap.cpp"
放在主文件中(以及其他需要它的地方),但是包含.cpp
文件是一个有争议的决定,它有更少争议的替代方案。
另一个选择是为所有你希望在Heap.cpp
中支持的类型预先实例化Heap
,它看起来像这样:
template class Heap<int>;
template class Heap<double>;
// etc
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用