我对“未定义的引用”错误有问题。我找不到解决办法来解决这个问题。我只是写了一个简单的AVL树程序。我的AVL.h
template <class Record>
class AVL_tree {
public:
AVL_tree();
~AVL_tree();
Error_code insert(const Record &new_data);
Error_code remove(const Record &old_data);
bool empty();
void printLNR();
private:
Error_code avl_insert(AVL_node<Record> *&sub_root,
const Record &new_data, bool &taller);
Error_code avl_remove(AVL_node<Record> *&sub_root,
const Record &old_data,bool &shorter);
void rotate_left(AVL_node<Record> *&sub_root);
void rotate_right(AVL_node<Record> *&sub_root);
void left_balance(AVL_node<Record> *&sub_root,bool &changeHeight);
void right_balance(AVL_node<Record> *&sub_root,bool &changeHeight);
AVL_node<Record> *root;
void destroy(AVL_node<Record> *);
void printLNR_recursive(AVL_node<Record> *);
};
我的AVL.cpp文件:
#include <iostream>
#include "AVL.h"
using namespace std;
//---------------------------------------------------------
template <class Record>
AVL_tree<Record>::AVL_tree()
{
root = NULL;
}
//---------------------------------------------------------
template <class Record>
AVL_tree<Record>::~AVL_tree()
{
destroy(root);
root = NULL;
}
//---------------------------------------------------------
template <class Record>
void AVL_tree<Record>::destroy(AVL_node<Record> *subroot) {
if (subroot != NULL) {
destroy(subroot->left);
destroy(subroot->right);
delete subroot;
}
}
我只是尝试在main.cpp中创建一个名为Sample的新obj,但是我得到了以下错误:
D:/AVL/main.cpp:8:对
AVL_tree<int>::AVL_tree()' D:/AVL/main.cpp:8: undefined reference to
AVL_tree::~AVL_tree()的未定义引用
我的main.cpp文件:
#include <iostream>
#include "AVL.h"
using namespace std;
int main() {
cout << "Hello, World!" << endl;
AVL_tree<int> Bee;
return 0;
}
发布于 2015-11-19 08:26:51
模板实现应该在主程序可见的标头中。如果您为程序中将要使用的所有类型编写专门化,则可以将实现放到源文件中。
https://stackoverflow.com/questions/33798491
复制相似问题