无法解析的外部符号

下午写了个模板类的小程序,在编译的时候出现了“无法解析的外部符号”的链接错误:

1> LINK : 没有找到 D:\ProgramData\VS2010Project\avl_tree\Debug\avl_tree.exe 或上一个增量链接没有生成它;正在执行完全链接 1>tesk.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall AvlTree<class Item<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~AvlTree<class Item<class std::basic_string<char,structstd::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" ([email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@@[email protected]),该符号在函数 __catch$_main{1} 中被引用 1>tesk.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall AvlTree<class Item<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::AvlTree<class Item<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class AvlTree<class Item<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > &)" ([email protected][email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@@[email protected]@@Z),该符号在函数 __catch$_main{1} 中被引用

用g++编译则错误信息如下:

无法解析的外部符号

经过几番修改尝试,都未找到症结所在。回头想想,由于对模板不是太熟,应该是在模板上出现了问题吧。

网上查找了一番,还果真如此:下面是摘抄网友的解决方法

1. 干脆直接使用包含模式,即将模板类的定义与实现同写在.h文件中。(我不太喜欢这种写法

2. 使用分离模式,但是在使用时不引用模板类的头文件,而是引用模板类的实现文件。(不太符合一般习惯)

3. 使用分离模式,在模板类头文件中引用实现文件。(这个方法我在VC6Dev C++下都没有成功,不知道是哪里出了问题)

4. 使用分离模式,在模板类中头文件中实例化一个你需要对象。(十分笨拙的方法,不利于使用)

5. 使用分离模式,但是另外定义.h文件,在这个文件中引用模板的头文件和实现文件,在使用时引用这个另外定义的.h文件。

我采用了第二种方法,编译成功。