Chinaunix

标题: 請教 template樣版 [打印本页]

作者: yych    时间: 2004-12-23 11:37
标题: 請教 template樣版
我定義了一個類
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)
好像是函數在外面定義時會出錯﹐請教為什么?
作者: converse    时间: 2004-12-23 12:41
标题: 請教 template樣版
缺少teacher的定义吧,猜想的,所以在你的函数中用到teacher成员的时候连接器找不到定义所以无法初始化这个变量
作者: benlan    时间: 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);
}
;
有分号吗?
作者: yych    时间: 2004-12-23 14:19
标题: 請教 template樣版
有teacher的定義﹐只是沒寫
編譯是過的
好像是那個函數定義的問題
我想問的是
用template時﹐在聲明外面定義函數該如何實現
并且template在某個類內部

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

不在聲明的同時定義﹐像上面這樣寫對嗎?
好像是有問題?﹗
作者: zealotcat    时间: 2004-12-23 14:55
标题: 請教 template樣版
把function_Call移到a.h中
作者: yych    时间: 2004-12-23 16:10
标题: 請教 template樣版
我也試過
但一方面這個函數定義太長﹐另外還會出現在一些別的錯
作者: gvim    时间: 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. }
复制代码

作者: benlan    时间: 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中




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2