essential C++第六章二叉树的问题?

firecityplans 2009-04-10 09:07:37
在将书的各个程序段(6.1—6.5)组织后编译出现了以下两个错误:
1。error LNK2001: unresolved external symbol "public: __thiscall BinaryTree<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::BinaryTree<class std::basic_string<char,struct std::char_traits<char>,clas

2。fatal error LNK1120: 1 unresolved externals

编译也能通过,但是链接就出现了这两个错误。不知为何,请教高手!
...全文
144 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
..搞定了?
「已注销」 2009-04-11
  • 打赏
  • 举报
回复
原因是缺少构造函数。
firecityplans 2009-04-11
  • 打赏
  • 举报
回复
楼上好眼力,我也在改,也看出来了。谢谢你啊。
「已注销」 2009-04-11
  • 打赏
  • 举报
回复
不好意思,是我搞错了,可以inline的。
#include <iostream>

using namespace std;

template <typename T>
class Test
{
public:
T f(T t)
{
return t * t;
}
};

template <typename T>
class Test2
{
public:
T f(T t);
};

template <typename T>
inline T Test2<T>::f(T t)
{
return t * t;
}

int main()
{
Test<int> t;
cout << t.f(2) << endl;

Test2<double> t2;
cout << t2.f(3.5) << endl;

return 0;
}
xylicon 2009-04-11
  • 打赏
  • 举报
回复
缺少默认构造函数

template < typename elemType >
inline BinaryTree < elemType >::BinaryTree(void)
{
// do sth here
}

加上这段就可以了。
「已注销」 2009-04-11
  • 打赏
  • 举报
回复
inline 是没有用的。
要显式的写在:
class {实现要写在这里};
「已注销」 2009-04-11
  • 打赏
  • 举报
回复
你用模板了,所以不要将实现写在外部。
gao125210 2009-04-11
  • 打赏
  • 举报
回复
up
liliangbao 2009-04-11
  • 打赏
  • 举报
回复
帮顶~
firecityplans 2009-04-11
  • 打赏
  • 举报
回复
第一个错误的全部错误显示如下:
error LNK2001: unresolved external symbol "public: __thiscall BinaryTree <class std::basic_string <char,struct std::char_traits <char>,class std::allocator <char> > >::BinaryTree <class std::basic_string <char,struct std::char_traits <char>,class std::allocator<char> > >(void)" (??0?$BinaryTree@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ)

程序完全的代码如下:

#include < iostream >
#include < vector >
#include < string >



using namespace std;


// class BTnode declare


template < typename elemType >
class BinaryTree;

template < typename valType >
class BTnode{
public:
friend class BinaryTree< valType >;
BTnode( const valType &val );
void insert_value( const valType &val );
void remove_value( const valType &val, BTnode *& prev );
void preorder ( BTnode *pt, ostream &os ) const;
void display_val( BTnode *pt, ostream &os ) const;
static void lchild_leaf( BTnode *leaf,BTnode *subtree );


private:
valType _val;
int _cnt;
BTnode *_lchild;
BTnode *_rchild;
};



// class BinaryTree declare

template < typename elemType >
class BinaryTree {
public:
BinaryTree();
BinaryTree( const BinaryTree& );
~BinaryTree();
BinaryTree& operator=( const BinaryTree&);
void insert( const elemType &elem );
bool empty()
{
return _root==0;
}
void remove( const elemType &elem );

void preorder() const
{
_root->preorder( _root, cout );
}
void remove_root();

void clear()
{
if( _root )
{
clear( _root );
_root = 0;
}
} //ostream &os = _current_os

friend ostream & operator<<( ostream &, const BinaryTree< elemType >);

private:
BTnode< elemType > *_root;
void copy( BTnode< elemType > * &, BTnode< elemType >* );
void clear( BTnode<elemType> * ); // static ostream *_current_os;
};

//BTnode define

template < typename valType >
inline BTnode< valType >::
BTnode( const valType &val ) : _val( val )
{
_cnt = 1;
_lchild = _rchild = 0;
}

template < typename valType >
void BTnode< valType >::
preorder ( BTnode *pt, ostream &os ) const
{
if ( pt )
{
display_val( pt ,os );
if ( pt->_lchild ) preorder( pt->_lchild, os );
if( pt->_rchild ) preorder( pt->_rchild, os );
}
}
template < typename valType >
void BTnode< valType >::
insert_value( const valType &val )
{
if( val == _val)
{
_cnt++;
return;
}
if( val < _val )
{
if( ! _lchild )
_lchild = new BTnode( val );
else
_lchild->insert_value( val );
}
else
{
if( ! _rchild )
_rchild = new BTnode( val );
else
_rchild->insert_value( val );
}
}

template < typename valType >
void BTnode< valType >::
remove_value( const valType &val, BTnode *& prev )
{
if( val < _val)
{
if( ! _lchild )
return;

else
_lchild->remove_value( val ,_lchild );
}
else
if( val > _val )
{
if( ! _rchild )
return;
else
_rchild->remove_value( val,_rchild );
}
else
{
if( _rchild )
{
prev = _rchild;
if( _lchild )
if( ! prev->_lchild )
prev->_lchild = _lchild;
else
BTnode< valType >::lchild_leaf( _lchild,prev->_lchild );
}
else prev = _lchild;
delete this;
}

}
template <typename valType>
inline void BTnode<valType>::
display_val( BTnode *pt, ostream &os ) const
{
os << pt->_val;
if ( pt->_cnt > 1 )
os << "( " << pt->_cnt << " ) ";
else os << ' ';
}
template < typename valType >
void BTnode< valType >::
lchild_leaf( BTnode *leaf, BTnode *subtree )
{
while ( subtree->_lchild )
subtree = subtree->_lchild;
subtree->_lchild = leaf;
}

// BinaryTree define
template < typename elemType >
inline BinaryTree< elemType >::BinaryTree( const BinaryTree &rhs )
{
copy( _root,rhs._root);
}

template < typename elemType >
inline BinaryTree< elemType >:: ~BinaryTree()
{
clear();
}

template < typename elemType >
inline BinaryTree < elemType >&
BinaryTree< elemType >::
operator=( const BinaryTree &rhs )
{
if ( this != &rhs )


{
clear();
copy(_root,rhs._root );
}

return *this;
}

template < typename elemType >
inline void BinaryTree< elemType >::
insert( const elemType &elem )
{
if( !_root )
_root = new BTnode< elemType >( elem );
else
_root->insert_value( elem );
}

template < typename elemType >
void BinaryTree< elemType >::
remove_root()
{
if( ! _root )
return ;
BTnode< elemType > *tmp = _root;
if( _root->_rchild )
{

_root = _root->_rchild;

if( tmp->_rchild )
{

BTnode< elemType > *lc = tmp->_lchild;
BTnode< elemType > *newlc = _root->_lchild;
if( !newlc )
_root->_lchild = lc;
else
BTnode <elemType>::lchild_leaf( lc,newlc );
}
}
else
_root = _root->_lchild;
delete tmp;
}

template < typename elemType >
void BinaryTree< elemType >::
remove( const elemType &elem )
{
if( _root )
{
if( _root->_val == elem )
remove_root();
else
_root->remove_value( elem, _root );
}
}


template < typename elemType >
void BinaryTree< elemType >::
clear( BTnode< elemType > *pt )
{
if( pt )
{
clear( pt->_lchild );
clear( pt->_rchild );
delete pt;
}
}
template < typename elemType > //BinaryTree< elemType >::
ostream & operator<<( ostream &os, const BinaryTree< elemType > &bt )
{
os << "Tree : "<< endl;
bt.print( os );

return os;
}

template <typename elemType>
void BinaryTree<elemType>::
copy( BTnode<elemType> * &tar, BTnode<elemType> *src )
{
if ( src )
{//new BTnode<elemType>( )
tar = src->_val ;
if ( src->_lchild ) copy( tar->_lchild, src->_lchild );
if ( src->_rchild ) copy( tar->_rchild, src->_rchild );
}
}

int main()
{
BinaryTree< string > bt;


bt.insert( " Piglet " );
bt.insert( "Eeyore" );
bt.insert( "Roo" );
bt.insert( "Tigger" );
bt.insert( "Chris" );
bt.insert( "Pooh" );
bt.insert( "Kanga" );

cout<< "Preorder traversal: \n";

bt.preorder();

bt.remove( "Piglet" );
cout<< " \n\n Preorder traversal after Piglet removal: \n";
bt.preorder();

bt.remove( "Eeyore" );
cout<< "\n\n Preorder traversal after Eeyore removal : \n";
bt.preorder();

return 0;
}



65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧