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樣版
//#include "a.h"
#include <iostream>;
using namespace std;
struct teacher
{
};
class student
{
public:
int a;
char* 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)
{
cout << "in func_Call"<<endl;
}
main()
{
student stu;
teacher struct1;
teacher teach;
int flag;
stu.function_Call(struct1,teach,flag);
}
复制代码
或者
//a.h
#include <iostream>;
using namespace std;
struct teacher
{
};
class student
{
public:
int a;
char* 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)
{
cout << "in func_Call"<<endl;
}
复制代码
//a.cpp
#include "a.h"
main()
{
student stu;
teacher struct1;
teacher teach;
int flag;
stu.function_Call(struct1,teach,flag);
}
复制代码
作者:
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