Error in constructor functions
This is the header file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
// Tree node structure
template <class T>
struct TreeNode
{
T data;
TreeNode *left, *right;
};
// delete all the nodes under the pointer
template <typename T>
void Delete(TreeNode<T>* ptr)
{
if(ptr!=NULL)
{
cout << "Delete one tree node..." << endl;
Delete(ptr->left);
Delete(ptr->right);
delete ptr;
}
}
// Binary tree class
template <typename T>
class BiTree
{
private:
TreeNode<T>* root;
public:
// constructors and destructor
BiTree();
BiTree(T dt);
BiTree(TreeNode<T>* pt);
~BiTree();
// access the root pointer
TreeNode<T>* Root() const;
// set the root
void Root(TreeNode<T>* ptr_node);
void Root(T dt);
// search (inorder, preorder, postorder)
void Search_Inorder() const;
void Search_Preorder() const;
void Search_Postorder() const;
};
|
This is the partial source file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
template <typename T>
BiTree<T>::BiTree()
{
root=NULL;
}
template <typename T>
BiTree<T>::BiTree(T dt)
{
//cout << "Assignment constructor..." << endl;
root=new TreeNode<T>;
root->data=dt;
root->left=NULL;
root->right=NULL;
}
template <typename T>
BiTree<T>::BiTree(TreeNode<T>* pt)
{
root=pt;
}
template <typename T>
BiTree<T>::~BiTree()
{
//cout << "Destructor..." << endl;
Delete(root);
}
|
When running the basic main sentence:
|
BiTree<double> bitr(1.0);
|
Xcode posted error like
Undefined symbols for architecture x86_64:
"BiTree<double>::BiTree(double)", referenced from:
Can anyone help me? Thank you!
Hi,
Template code must go into a header file, see if that helps - I haven't looked at the code to see if anything else is going on.
Cheers :+)
Topic archived. No new replies allowed.