一个让我崩溃的错误
Binary_tree.h
C/C++ code
Binary_tree.cpp
C/C++ code
Main.cpp
C/C++ code
不是我自己写的代码,是老师PPt上拷贝的,同学拷贝的没有错误,我拷过来就有错误,错误如下:
Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Binary_node<int>::Binary_node<int>(int const &)" (??0?$Binary_node@H@@QAE@ABH@Z)
Debug/Binary_tree.exe : fatal error LNK1120: 1 unresolved externals
大概是说我的那个重载的构造函数找不到,但是我将那个构造函数
template <class Entry>
Binary_tree<Entry>::Binary_tree(const Binary_tree<Entry> &x)
{
root=recursive_copy(x.root);
}
的哪一行 root=recursive_copy(x.root);屏蔽掉,然后编译,错误;删掉debug内容,编译,通过,运行,出错;恢复那一行,删掉debug,编译通过,运行出错;快崩溃了。换了一台电脑,用2010编译(之前是6.0),同样的错误。崩溃了……
C/C++ code
#ifndef Binary_Tree #define Binary_Tree //Node类 template <class Entry> struct Binary_node { // data members: Entry data; Binary_node<Entry> *left; Binary_node<Entry> *right; // constructors: Binary_node( ); Binary_node(const Entry &x); }; //Binary_Tree类 template <class Entry> class Binary_tree { public: Binary_tree(); Binary_tree(const Binary_tree<Entry> &x); ~Binary_tree(); Binary_tree & operator = (const Binary_tree<Entry> &original); bool empty() const; void preorder(void (*visit)(Entry &)); void inorder(void (*visit)(Entry &)); void postorder(void (*visit)(Entry &)); int size( ) const; void clear( ); int height( ) const; void insert(const Entry &); protected: Binary_node<Entry> *root; void recursive_inorder(Binary_node<Entry> *sub_root,void (*visit)(Entry &)); int recursive_size(Binary_node<Entry> *sub_root) const; int recursive_height(Binary_node<Entry> *sub_root) const; Binary_node<Entry> *recursive_copy(Binary_node<Entry> *sub_root); }; #endif
Binary_tree.cpp
C/C++ code
#include "Binary_Tree.h" #include "iostream" using namespace std; template <class Entry> Binary_tree<Entry>::Binary_tree() { root=NULL; } template <class Entry> Binary_tree<Entry>::Binary_tree(const Binary_tree<Entry> &x) { root=recursive_copy(x.root); } template <class Entry> Binary_tree<Entry>::~Binary_tree() { root=NULL; } template <class Entry> bool Binary_tree<Entry> :: empty( ) const { return root == NULL; } template <class Entry> int Binary_tree<Entry>::size() const { return recursive_size(root); } template <class Entry> int Binary_tree<Entry>::height() const { return recursive_height(root); } template <class Entry> int Binary_tree<Entry> :: recursive_height(Binary_node<Entry> *sub_root) const { if (sub_root == NULL) return 0; int l = recursive_height(sub_root->left); int r = recursive_height(sub_root->right); if (l > r) return 1 + l; else return 1 +r; } template <class Entry> void Binary_tree<Entry> :: inorder(void (*visit)(Entry &)) { recursive_inorder(root, visit); } template <class Entry> void Binary_tree<Entry> ::recursive_inorder(Binary_node<Entry> *sub_root,void (*visit)(Entry &)) { if (sub_root != NULL) { recursive_inorder(sub_ root->left, visit); (*visit)(sub_root->data); recursive_inorder(sub_root->right, visit); } } template <class Entry> int Binary_tree<Entry> :: recursive_size(Binary_node<Entry> *sub_root) const { if (sub_root == NULL) return 0; return 1+recursive_size(sub_root->left)+ recursive_size(sub_root->right); } template <class Entry> Binary_node<Entry> *Binary_tree<Entry> :: recursive_copy(Binary_node<Entry> *sub_root) { cout << "执行了copy函数" << endl; if (sub_root == NULL) return NULL; Binary_node<Entry> *temp = new Binary_node<Entry>(sub_root->data); temp->left = recursive_copy(sub_root->left); temp->right = recursive_copy(sub_root->right); return temp; }
Main.cpp
C/C++ code
#include "iostream" #include "Binary_Tree.cpp" using namespace std; void main() { Binary_tree<int> my; cout << my.size() << endl; Binary_tree<int> my1(my); cout << my1.size(); Binary_tree<int> my2(my); }
不是我自己写的代码,是老师PPt上拷贝的,同学拷贝的没有错误,我拷过来就有错误,错误如下:
Main.obj : error LNK2001: unresolved external symbol "public: __thiscall Binary_node<int>::Binary_node<int>(int const &)" (??0?$Binary_node@H@@QAE@ABH@Z)
Debug/Binary_tree.exe : fatal error LNK1120: 1 unresolved externals
大概是说我的那个重载的构造函数找不到,但是我将那个构造函数
template <class Entry>
Binary_tree<Entry>::Binary_tree(const Binary_tree<Entry> &x)
{
root=recursive_copy(x.root);
}
的哪一行 root=recursive_copy(x.root);屏蔽掉,然后编译,错误;删掉debug内容,编译,通过,运行,出错;恢复那一行,删掉debug,编译通过,运行出错;快崩溃了。换了一台电脑,用2010编译(之前是6.0),同样的错误。崩溃了……
作者: cjqpker 发布时间: 2011-06-16
C/C++ code
把struct改为class,或者添加public:
template <class Entry> struct Binary_node { // data members: Entry data; Binary_node<Entry> *left; Binary_node<Entry> *right; // constructors: Binary_node( ); Binary_node(const Entry &x); };
把struct改为class,或者添加public:
作者: hustlaofan 发布时间: 2011-06-16
#include "iostream"
#include "Binary_Tree.cpp"???
应该是 #include "Binary_Tree.h" 吧?
#include "Binary_Tree.cpp"???
应该是 #include "Binary_Tree.h" 吧?
作者: johnroot 发布时间: 2011-06-16