免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1059 | 回复: 7
打印 上一主题 下一主题

請教 template樣版 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-23 11:37 |只看该作者 |倒序浏览
我定義了一個類
a.h中
struct teacher
{
}
class student
{
Public:
  int a;
  CString str;
  template<class Y>;
  void function_Call(Y & struct1, teacher & teach,int);
}
a.CPP中
template<class Y>;//Y為不同的類
void student::function_Call(Y & struct1, teacher & teach,int flag)
{
}
main()
{
student stu;
stu.function_Call(struct1,teach,flag);
}
這些編譯時沒問題﹐但link時有錯誤
error LNK2001: unresolved external symbol "public: void __thiscall student::Function_Call(struct M_SpecParameter&,struct teacher&,int)" (?Switch@CClass_FunctionCall@@QAEXAAUM_SpecParameter@@AAUteacher@H@Z)
好像是函數在外面定義時會出錯﹐請教為什么?

论坛徽章:
0
2 [报告]
发表于 2004-12-23 12:41 |只看该作者

請教 template樣版

缺少teacher的定义吧,猜想的,所以在你的函数中用到teacher成员的时候连接器找不到定义所以无法初始化这个变量

论坛徽章:
0
3 [报告]
发表于 2004-12-23 13:23 |只看该作者

請教 template樣版

struct teacher
{
}
;

class student
{
Public:
int a;
CString str;
template<class Y>;
void function_Call(Y & struct1, teacher & teach,int);
}
;
有分号吗?

论坛徽章:
0
4 [报告]
发表于 2004-12-23 14:19 |只看该作者

請教 template樣版

有teacher的定義﹐只是沒寫
編譯是過的
好像是那個函數定義的問題
我想問的是
用template時﹐在聲明外面定義函數該如何實現
并且template在某個類內部

template<class Y>;//Y為不同的類
void student::function_Call(Y & struct1, teacher & teach,int flag)
{
}

不在聲明的同時定義﹐像上面這樣寫對嗎?
好像是有問題?﹗

论坛徽章:
0
5 [报告]
发表于 2004-12-23 14:55 |只看该作者

請教 template樣版

把function_Call移到a.h中

论坛徽章:
0
6 [报告]
发表于 2004-12-23 16:10 |只看该作者

請教 template樣版

我也試過
但一方面這個函數定義太長﹐另外還會出現在一些別的錯

论坛徽章:
2
亥猪
日期:2014-03-19 16:36:35午马
日期:2014-11-23 23:48:46
7 [报告]
发表于 2004-12-23 19:05 |只看该作者

請教 template樣版


  1. //#include "a.h"
  2. #include <iostream>;

  3. using namespace std;

  4. struct teacher
  5. {
  6. };

  7. class student
  8. {
  9. public:
  10.         int a;
  11.         char* str;
  12.         template<class Y>;
  13.         void function_Call(Y& struct1, teacher& teach,int);
  14. };

  15. template<class Y>;//Y為不同的類
  16. void student::function_Call(Y& struct1, teacher& teach,int flag)
  17. {
  18.         cout << "in func_Call"<<endl;
  19. }
  20. main()
  21. {
  22.         student stu;
  23.         teacher struct1;
  24.         teacher teach;
  25.         int flag;
  26.         stu.function_Call(struct1,teach,flag);
  27. }
复制代码


或者

  1. //a.h
  2. #include <iostream>;

  3. using namespace std;

  4. struct teacher
  5. {
  6. };

  7. class student
  8. {
  9. public:
  10.         int a;
  11.         char* str;
  12.         template<class Y>;
  13.         void function_Call(Y& struct1, teacher& teach,int);
  14. };

  15. template<class Y>;//Y為不同的類
  16. void student::function_Call(Y& struct1, teacher& teach,int flag)
  17. {
  18.         cout << "in func_Call"<<endl;
  19. }
复制代码


  1. //a.cpp
  2. #include "a.h"
  3. main()
  4. {
  5.         student stu;
  6.         teacher struct1;
  7.         teacher teach;
  8.         int flag;
  9.         stu.function_Call(struct1,teach,flag);
  10. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2004-12-24 09:58 |只看该作者

請教 template樣版

class student
{
Public:
int a;
CString str;
template<class Y>;
void function_Call(Y & struct1, teacher & teach,int);
}

template<class Y>;//Y為不同的類
void student::function_Call(Y & struct1, teacher & teach,int flag)
{
}
template<class Y>;不一致了



a.h改为
template<class Y>;
class student
{
Public:
int a;
CString str;
void function_Call(Y & struct1, teacher & teach,int);
}

a.cpp里的template<class Y>;去掉。
或象上楼说的:
把function_Call移到a.h中
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP