我实现了一个AVL树作为库的一部分,并且无法弄清楚它是CppUnit,NetBeans 8,我的C ++技能(模板)还是其他什么问题,但是代码编译为静态库但是当与CppUnit或独立应用程序一起使用时,我得到undefined reference to MyProject::Utilities::AVLTree<int, char>::function
。
我在保持相同错误的同时尽可能地减少了代码。
AVLTree.h
namespace MyProject {
namespace Utilities {
template <typename KeyType, typename ValueType> class AVLTree {
public:
AVLTree();
AVLTree(const MyProject::Utilities::AVLTree<KeyType, ValueType>& orig);
virtual ~AVLTree();
bool hasRoot() const;
MyProject::Utilities::AVLNode<KeyType, ValueType>* getRoot() const;
protected:
void setRoot(MyProject::Utilities::AVLNode<KeyType, ValueType>* _node);
private:
MyProject::Utilities::AVLNode<KeyType, ValueType>* _root;
unsigned int _size;
};
};
};
AVLTree.cpp
template <typename KeyType, typename ValueType> MyProject::Utilities::AVLTree<KeyType, ValueType>::AVLTree() {
this->setRoot(NULL);
this->_size = 0;
}
template <typename KeyType, typename ValueType> MyProject::Utilities::AVLTree<KeyType, ValueType>::AVLTree(const MyProject::Utilities::AVLTree<KeyType, ValueType>& orig) {
this->setRoot(orig.getRoot());
this->_size = orig.size();
}
template <typename KeyType, typename ValueType> MyProject::Utilities::AVLTree<KeyType, ValueType>::~AVLTree() {
// this->clear();
}
template <typename KeyType, typename ValueType> void MyProject::Utilities::AVLTree<KeyType, ValueType>::setRoot(MyProject::Utilities::AVLNode<KeyType, ValueType>* _root) {
this->_root = _root;
}
template <typename KeyType, typename ValueType> bool MyProject::Utilities::AVLTree<KeyType, ValueType>::hasRoot() const {
return this->getRoot() != NULL;
}
template <typename KeyType, typename ValueType> MyProject::Utilities::AVLNode<KeyType, ValueType>* MyProject::Utilities::AVLTree<KeyType, ValueType>::getRoot() const {
return this->_root;
}
的main.cpp
#include <cstdlib>
#include "AVLTree.h"
int main(int argc, char** argv) {
MyProject::Utilities::AVLTree<int, char> instance;
for (unsigned int i = 0; i < 20; ++i) {
instance.hasRoot();
// instance.insert(i, (char) (65 + i));
}
//instance.graphviz("./test.dot");
return 0;
}
Netbeans 8在尝试构建cppapplication_1
时返回以下错误g++ -c -g -I../../Documents/MyProject/src/header -MMD -MP -MF "build/Debug/GNU-Linux-x86/main.o.d" -o build/Debug/GNU-Linux-x86/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux-x86
g++ -o dist/Debug/GNU-Linux-x86/cppapplication_1 build/Debug/GNU-Linux-x86/main.o -L../../Documents/MyProject/dist/Debug/GNU-Linux-x86 -lmyproject
build/Debug/GNU-Linux-x86/main.o: In function `main':
/home/.../CppApplication_1/main.cpp:6: undefined reference to `MyProject::Utilities::AVLTree<int, char>::AVLTree()'
/home/.../CppApplication_1/main.cpp:8: undefined reference to `MyProject::Utilities::AVLTree<int, char>::hasRoot() const'
/home/.../CppApplication_1/main.cpp:12: undefined reference to `MyProject::Utilities::AVLTree<int, char>::~AVLTree()'
/home/.../CppApplication_1/main.cpp:12: undefined reference to `MyProject::Utilities::AVLTree<int, char>::~AVLTree()'
collect2: error: ld returned 1 exit status
以下代码会产生大约100个不同内容的错误......我不是故意愚蠢但是对于我的生活,我无法弄清楚出了什么问题,也找不到工作使用的示例或教程...
src/header/AVLTree.h:16:23: error: declaration of ‘class KeyType’
template <typename KeyType, typename ValueType> AVLTree();
src/header/AVLTree.h:14:19: error: shadows template parm ‘class KeyType’
template <typename KeyType, typename ValueType> class AVLTree {
src/header/AVLTree.h:16:41: error: declaration of ‘class ValueType’
template <typename KeyType, typename ValueType> AVLTree();
src/header/AVLTree.h:14:37: error: shadows template parm ‘class ValueType’
template <typename KeyType, typename ValueType> class AVLTree {
In file included from src/AVLTree.cpp:1:0:
src/header/AVLTree.h:18:23: error: declaration of ‘class KeyType’
template <typename KeyType, typename ValueType> virtual ~AVLTree();
AVLTree.h
namespace MyProject {
namespace Utilities {
template <typename KeyType, typename ValueType> class AVLTree {
public:
template <typename KeyType, typename ValueType> AVLTree();
template <typename KeyType, typename ValueType> AVLTree(const MyProject::Utilities::AVLTree<KeyType, ValueType>& orig);
template <typename KeyType, typename ValueType> virtual ~AVLTree();
template <typename KeyType, typename ValueType> bool hasRoot() const;
template <typename KeyType, typename ValueType> MyProject::Utilities::AVLNode<KeyType, ValueType>* getRoot() const;
protected:
void setRoot(MyProject::Utilities::AVLNode<KeyType, ValueType>* _node);
private:
MyProject::Utilities::AVLNode<KeyType, ValueType>* _root;
};
};
};
AVLTree.cpp(指定了类型)
MyProject::Utilities::AVLTree<KeyType, ValueType>::AVLTree() {
this->setRoot(NULL);
}
MyProject::Utilities::AVLTree<KeyType, ValueType>::AVLTree(const MyProject::Utilities::AVLTree<KeyType, ValueType>& orig) {
this->setRoot(orig.getRoot());
// this->_size = orig.size();
}
MyProject::Utilities::AVLTree<KeyType, ValueType>::~AVLTree() {
// this->clear();
}
void MyProject::Utilities::AVLTree<KeyType, ValueType>::setRoot(MyProject::Utilities::AVLNode<KeyType, ValueType>* _root) {
this->_root = _root;
}
bool MyProject::Utilities::AVLTree<KeyType, ValueType>::hasRoot() const {
return this->getRoot() != NULL;
}
MyProject::Utilities::AVLNode<KeyType, ValueType>* MyProject::Utilities::AVLTree<KeyType, ValueType>::getRoot() const {
return this->_root;
}
AVLTree.cpp(未指定类型)
MyProject::Utilities::AVLTree::AVLTree() {
this->setRoot(NULL);
}
MyProject::Utilities::AVLTree::AVLTree(const MyProject::Utilities::AVLTree& orig) {
this->setRoot(orig.getRoot());
// this->_size = orig.size();
}
MyProject::Utilities::AVLTree::~AVLTree() {
// this->clear();
}
void MyProject::Utilities::AVLTree::setRoot(MyProject::Utilities::AVLNode<KeyType, ValueType>* _root) {
this->_root = _root;
}
bool MyProject::Utilities::AVLTree::hasRoot() const {
return this->getRoot() != NULL;
}
MyProject::Utilities::AVLNode<KeyType, ValueType>* MyProject::Utilities::AVLTree::getRoot() const {
return this->_root;
}
答案 0 :(得分:0)
模板不是功能。它们不存在于代码中。它们只是一些蓝图,或者说明了在知道所有模板参数后如何实际生成函数的说明。因此,所有模板都必须驻留在头文件中,以便在知道所有模板参数后可见。 将所有模板代码从AVLTree.cpp移动到AVLTree.h 。仅仅移动还不够,还必须改变它。
以下是在我的Visual Studio上编译的示例:
namespace My {
namespace Util {
template <typename KeyType, typename ValueType> class World {
public:
World()
{
}
World(const My::Util::World<KeyType, ValueType>& orig)
{
}
virtual ~World()
{
}
bool Hello() const
{
return true;
}
};
};
};
使用如下代码:
My::Util::World<int, char> world;
world.Hello();
可以将所有函数与类声明分开,但它没有任何实际意义,因为所有分离的代码无论如何都必须是可见的。它看起来像这样:
namespace My {
namespace Util {
template <typename KeyType, typename ValueType> class World {
....
bool Hello() const;
};
};
};
template<typename KeyType, typename ValueType>
bool My::Util::World<KeyType, ValueType>::Hello() const
{
return true;
}
我试过你的例子,它对我有用。我刚刚在类模板之后放置了所有函数模板,并注释掉了所有处理AVLNode
的代码,并进行了编译。所以我的原始陈述似乎是有效的:只需将所有模板代码从源移动到标头。您的第二个示例代码与您的第一个代码不同。你应该做的只是搬家。