关于模板类的编译错误
我有一个这样的AVL课程,它是模板的,并且会出现错误,我试图调试它数小时,但没有固定。有什么问题?我缺少什么部分?它可能与构造函数有关,但我尝试过,但没有解决。我正在使用Visual Studio 2012,我感到卡住了。
错误是:
1>Ege_HW2.obj : error LNK2019: unresolved external symbol "public: __thiscall AvlTree<struct item>::~AvlTree<struct item>(void)" (??1?$AvlTree@Uitem@@@@QAE@XZ) referenced in function __unwindfunclet$??0AVLSection@@QAE@XZ$0
1>Ege_HW2.obj : error LNK2019: unresolved external symbol "public: __thiscall AvlTree<struct AVLSection>::~AvlTree<struct AVLSection>(void)" (??1?$AvlTree@UAVLSection@@@@QAE@XZ) referenced in function _main
1>fatal error LNK1120: 2 unresolved externals
标题文件:
#ifndef AVLTREE_H
#define AVLTREE_H
#include <iostream>
#include <string>
using namespace std;
/* AVL Tree is taken from the lecture slides. */
template <class Comparable>
class AvlTree;
template <class Comparable>
class AvlNode
{
Comparable element;
AvlNode* left;
AvlNode* right;
int height;
AvlNode(const Comparable& theElement,
AvlNode* lt, AvlNode* rt, int h = 0)
: element(theElement), left(lt), right(rt), height(h) { }
friend class AvlTree<Comparable>;
};
template <class Comparable>
class AvlTree
{
public:
explicit AvlTree();
explicit AvlTree(const Comparable& notFound);
AvlTree(const AvlTree& rhs);
~AvlTree();
const Comparable& findMin() const;
const Comparable& findMax() const;
const Comparable& find(const Comparable& x) const;
bool isEmpty() const;
void printTree() const;
void makeEmpty();
void insert(const Comparable& x);
void remove(const Comparable& x);
const AvlTree& operator=(const AvlTree& rhs);
private:
AvlNode<Comparable>* root;
const Comparable ITEM_NOT_FOUND;
const Comparable& elementAt(AvlNode<Comparable>* t) const;
void insert(const Comparable& x, AvlNode<Comparable>*& t) const;
void remove(const Comparable& x, AvlNode<Comparable>*& t) const;
AvlNode<Comparable>* findMin(AvlNode<Comparable>* t) const;
AvlNode<Comparable>* findMax(AvlNode<Comparable>* t) const;
AvlNode<Comparable>* find(const Comparable& x, AvlNode<Comparable>* t) const;
void makeEmpty(AvlNode<Comparable>*& t) const;
void printTree(AvlNode<Comparable>* t) const;
AvlNode<Comparable>* clone(AvlNode<Comparable>* t) const;
// Avl manipulations
int height(AvlNode<Comparable>* t) const;
int max(int lhs, int rhs) const;
void rotateWithLeftChild(AvlNode<Comparable>*& k2) const;
void rotateWithRightChild(AvlNode<Comparable>*& k1) const;
void doubleWithLeftChild(AvlNode<Comparable>*& k3) const;
void doubleWithRightChild(AvlNode<Comparable>*& k1) const;
};
#include "AvlTree.cpp"
#endif
CPP文件:
#include "avlTree.h"
#include <iostream>
using namespace std;
template <class Comparable>
AvlTree<Comparable>::AvlTree(){
root = NULL;
}
/*** Construct the tree.
*/
template <class Comparable>
AvlTree<Comparable>::AvlTree(const Comparable& notFound)
:ITEM_NOT_FOUND(notFound), root(NULL){}
/*** Copy constructor.*/
template <class Comparable>
AvlTree<Comparable>::AvlTree(const AvlTree<Comparable>& rhs)
: root(NULL), ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND) {
*this = rhs;
}
最后是主要:
#include <iostream>
#include <string>
#include "BinaryTree.h"
#include "AvlTree.h"
#include <fstream>
using namespace std;
struct item{
string title;
string information;
item(){};
};
struct AVLSection{
AvlTree<item> avlSection;
string sectionTitle;
AVLSection(){};
};
struct BSTSection{
BinarySearchTree <item> bstSection;
string sectionTitle;
BSTSection(){}
};
void readData(BinarySearchTree<BSTSection> & bst, AvlTree<AVLSection> & avl){
ifstream input;
string filename = "data.txt",line;
input.open(filename.c_str());
while(getline(input,line)){
cout << line;
}
}
int main(){
BinarySearchTree<BSTSection> bst;
AvlTree<AVLSection> avl;
}
I have a AVL class like this, it's templated and it gives error in main, I have tried to debug it for hours but it didn't fixed. What can be the problem? What part am I missing? It can be related to constructors but I tried that and it didn't fixed. I'm using Visual Studio 2012 and I feel stuck.
The error is:
1>Ege_HW2.obj : error LNK2019: unresolved external symbol "public: __thiscall AvlTree<struct item>::~AvlTree<struct item>(void)" (??1?$AvlTree@Uitem@@@@QAE@XZ) referenced in function __unwindfunclet$??0AVLSection@@QAE@XZ$0
1>Ege_HW2.obj : error LNK2019: unresolved external symbol "public: __thiscall AvlTree<struct AVLSection>::~AvlTree<struct AVLSection>(void)" (??1?$AvlTree@UAVLSection@@@@QAE@XZ) referenced in function _main
1>fatal error LNK1120: 2 unresolved externals
Header file:
#ifndef AVLTREE_H
#define AVLTREE_H
#include <iostream>
#include <string>
using namespace std;
/* AVL Tree is taken from the lecture slides. */
template <class Comparable>
class AvlTree;
template <class Comparable>
class AvlNode
{
Comparable element;
AvlNode* left;
AvlNode* right;
int height;
AvlNode(const Comparable& theElement,
AvlNode* lt, AvlNode* rt, int h = 0)
: element(theElement), left(lt), right(rt), height(h) { }
friend class AvlTree<Comparable>;
};
template <class Comparable>
class AvlTree
{
public:
explicit AvlTree();
explicit AvlTree(const Comparable& notFound);
AvlTree(const AvlTree& rhs);
~AvlTree();
const Comparable& findMin() const;
const Comparable& findMax() const;
const Comparable& find(const Comparable& x) const;
bool isEmpty() const;
void printTree() const;
void makeEmpty();
void insert(const Comparable& x);
void remove(const Comparable& x);
const AvlTree& operator=(const AvlTree& rhs);
private:
AvlNode<Comparable>* root;
const Comparable ITEM_NOT_FOUND;
const Comparable& elementAt(AvlNode<Comparable>* t) const;
void insert(const Comparable& x, AvlNode<Comparable>*& t) const;
void remove(const Comparable& x, AvlNode<Comparable>*& t) const;
AvlNode<Comparable>* findMin(AvlNode<Comparable>* t) const;
AvlNode<Comparable>* findMax(AvlNode<Comparable>* t) const;
AvlNode<Comparable>* find(const Comparable& x, AvlNode<Comparable>* t) const;
void makeEmpty(AvlNode<Comparable>*& t) const;
void printTree(AvlNode<Comparable>* t) const;
AvlNode<Comparable>* clone(AvlNode<Comparable>* t) const;
// Avl manipulations
int height(AvlNode<Comparable>* t) const;
int max(int lhs, int rhs) const;
void rotateWithLeftChild(AvlNode<Comparable>*& k2) const;
void rotateWithRightChild(AvlNode<Comparable>*& k1) const;
void doubleWithLeftChild(AvlNode<Comparable>*& k3) const;
void doubleWithRightChild(AvlNode<Comparable>*& k1) const;
};
#include "AvlTree.cpp"
#endif
Cpp file:
#include "avlTree.h"
#include <iostream>
using namespace std;
template <class Comparable>
AvlTree<Comparable>::AvlTree(){
root = NULL;
}
/*** Construct the tree.
*/
template <class Comparable>
AvlTree<Comparable>::AvlTree(const Comparable& notFound)
:ITEM_NOT_FOUND(notFound), root(NULL){}
/*** Copy constructor.*/
template <class Comparable>
AvlTree<Comparable>::AvlTree(const AvlTree<Comparable>& rhs)
: root(NULL), ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND) {
*this = rhs;
}
And finally the main:
#include <iostream>
#include <string>
#include "BinaryTree.h"
#include "AvlTree.h"
#include <fstream>
using namespace std;
struct item{
string title;
string information;
item(){};
};
struct AVLSection{
AvlTree<item> avlSection;
string sectionTitle;
AVLSection(){};
};
struct BSTSection{
BinarySearchTree <item> bstSection;
string sectionTitle;
BSTSection(){}
};
void readData(BinarySearchTree<BSTSection> & bst, AvlTree<AVLSection> & avl){
ifstream input;
string filename = "data.txt",line;
input.open(filename.c_str());
while(getline(input,line)){
cout << line;
}
}
int main(){
BinarySearchTree<BSTSection> bst;
AvlTree<AVLSection> avl;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论