|
![]() | 作者: BBL [bbl]
![]() |
登录 |
类的声明文件--Example.h --------------------- #pragma once template <class Ex> class Example { public: Example(void); bool scan(const Ex); bool print(); public: ~Example(void); private: Ex value; }; ===================== 类的定义文件--Example.cpp --------------------- #include "StdAfx.h" #include "Example.h" #include "iostream" using namespace std; template <class Ex> Example<Ex>::Example(void) { } template <class Ex> bool Example<Ex>::scan(const Ex temp) { value=temp; return true; } template <class Ex> bool Example<Ex>::print() { cout<<value<<endl; return true; } template <class Ex> Example<Ex>::~Example(void) { } ==================== main文件 -------------------- #include "stdafx.h" #include "iostream" #include "Example.h" using namespace std; int _tmain(int argc, _TCHAR* argv[]) { Example<double> Ex1; double aa=16.1; Ex1.scan(aa); Ex1.print(); } ====================================== 我是用visual c++ 2003的..但为什么老提示 文件名.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Example<double>::~Example<double>(void)" (??1?$Example@N@@QAE@XZ),该符号在函数 _wmain 中被引用 文件名.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall Example<double>::print(void)" (?print@?$Example@N@@QAE_NXZ),该符号在函数 _wmain 中被引用 文件名.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall Example<double>::scan(double)" (?scan@?$Example@N@@QAE_NN@Z),该符号在函数 _wmain 中被引用 文件名.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Example<double>::Example<double>(void)" (??0?$Example@N@@QAE@XZ),该符号在函数 _wmain 中被引用 D:\C++ 项目目录\47\Debug\文件名.exe : fatal error LNK1120: 4 个无法解析的外部命令 ==================================================================================== 哈哈哈..我已经解决了:原来是有时方便反而成了不方便 解决:原来是我把声明与定义分开为头文件与源文件了,在模板类/函数的声明与定义都必须在同一文件内,否则就会出现"error:LNK2019:无法解析的外部命令" ===================================================== 我给搞了两天一直弄不清原因,直到最后在网上查了好久才查到的,分享下,以后的跟我一样的C++初学者在用visual studio c++时使用模板类才可借鉴下... 太高兴了...哈哈哈 ![]() [此贴被 BBL(bbl) 在 10月13日22时31分 编辑过] |
地主 发表时间: 06-10-11 23:11 |
![]() | 回复: jhkdiy [jhkdiy] ![]() |
登录 |
原来是这样啊,学习了。 |
B1层 发表时间: 06-10-16 01:27 |
![]() | 回复: BBL [bbl] ![]() |
登录 |
哈哈哈..还是得益于有得上网...万岁..上网基本能查到时所以的问题..哈哈哈 |
B2层 发表时间: 06-10-16 17:29 |
![]() | 回复: zlcolin [zlcolin] ![]() |
登录 |
呵呵,只不过是不能把声明和定义分别放在.hpp文件和.cpp文件,但你可以把定义也放在.hpp里面呀,这样就不会有问题了,而且和你原来的效果一样。 |
B3层 发表时间: 07-04-17 12:14 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号