gpt4 book ai didi

c++ - 使用 BOOST 时出现 LNK 错误 1561

转载 作者:行者123 更新时间:2023-11-28 03:22:01 26 4
gpt4 key购买 nike

我正在尝试创建一个 avl 树,然后使用 boost 来检查我创建的内容是否正常工作。但是,在我所有的 boost 测试用例中,我没有一个主要的(我认为这是导致问题的问题)。这是我的 avl.hpp我对 C++ 有点陌生。

template <typename T>
class avlTreeNode
{
public:
// data in the node
T nodeValue;
// pointers to the left and right children of the node
avlTreeNode<T> *left;
avlTreeNode<T> *right;
int balanceFactor;
// CONSTRUCTOR
avlTreeNode (const T& item, avlTreeNode<T> *lptr = NULL,
avlTreeNode<T> *rptr = NULL, int bal = 0):
nodeValue(item), left(lptr), right(rptr), balanceFactor(bal)
{
}
};

const int leftheavy = -1;
const int balanced = 0;
const int rightheavy = 1;

template <typename T>
class avlTree
{
public:
// CONSTRUCTORS, DESTRUCTOR, ASSIGNMENT
// constructor. initialize root to NULL and size to 0
avlTree();
~avlTree();

typedef T* iterator;
typedef T const* const_iterator;
// constructor. insert n elements from range of T* pointers4
avlTree(T *first, T *last);
// search for item. if found, return an iterator pointing
// at it in the tree; otherwise, return end()
iterator find(const T& item);
// search for item. if found, return an iterator pointing
// at it in the tree; otherwise, return end()
const_iterator find(const T& item) const;
// indicate whether the tree is empty
int empty() const;
// return the number of data items in the tree
int size() const;
// give a vertical display of the tree .
void displayTree(int maxCharacters) const;
// insert item into the tree
//std::pair<iterator, bool> insert(const T& item);
// insert a new node using the basic List operation and format
//std::pair<iterator, bool> insert(const T& item);
// delete all the nodes in the tree
void clear();
// constant versions
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;

///////////////////////////////////////////////////////////
//when I created this it started giving me the lnk 1561 error
std::pair<iterator,bool> insert(const T& item)
{
avlTree<T>::iterator iter;
// quietly return if item is already in the tree
if ((iter = find(item)) != end() )
return std::pair<iterator,bool>(iter,false);
// declare AVL tree node pointers.
avlTreeNode<T> *treeNode = root,*newNode;
// flag used by AVLInsert to rebalance nodes
bool reviseBalanceFactor = false;
// get a new AVL tree node with empty pointer fields
newNode = getavlTreeNode(item,NULL,NULL);
// call recursive routine to actually insert the element
avlInsert(treeNode, newNode, reviseBalanceFactor);
// assign new values to data members root, size and current
root = treeNode;
treeSize++;
return std::pair<iterator, bool> (iterator(newNode), true);
}

private:
// pointer to tree root
avlTreeNode<T> *root;
// number of elements in the tree
int treeSize;
// allocate a new tree node and return a pointer to it
avlTreeNode<T> *getavlTreeNode(const T& item,
avlTreeNode<T> *lptr,avlTreeNode<T> *rptr);
// used by copy constructor and assignment operator
avlTreeNode<T> *copyTree(avlTreeNode<T> *t);
// delete the storage occupied by a tree node
void freeavlTreeNode(avlTreeNode<T> *p);
// used by destructor, assignment operator and clear()
void deleteTree(avlTreeNode<T> *t);
// locate a node item and its parent in tree. used by find()
avlTreeNode<T> *findNode(const T& item,
avlTreeNode<T>* & parent) const;
// member functions to insert and erase a node
void singleRotateLeft (avlTreeNode<T>* &p);
void singleRotateRight (avlTreeNode<T>* &p);
void doubleRotateLeft (avlTreeNode<T>* &p);
void doubleRotateRight (avlTreeNode<T>* &p);
void updateLeftTree (avlTreeNode<T>* &tree,
bool &reviseBalanceFactor);
void updateRightTree (avlTreeNode<T>* &tree,
bool &reviseBalanceFactor);
};

最佳答案

在您的 cpp 文件的顶部,定义 BOOST_TEST_MAIN。用于单元测试库自动生成main函数。

例如:

#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include "avl.hpp"

BOOST_AUTO_TEST_CASE( test )
{
// ...
}

关于c++ - 使用 BOOST 时出现 LNK 错误 1561,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com