如何定义模板类的模板成员函数?

a_hui123 2003-03-27 06:12:06
template <typename X>
class C
{
public:
template <typename Y> void f(Y* p);
private:
int m_m;
};
如何在类C 的外面定义函数f()呢?

...全文
210 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_hui123 2003-03-28
  • 打赏
  • 举报
回复
谢谢大家,后悔当初太小气,这个题目才给了20分。
chinajiji 2003-03-27
  • 打赏
  • 举报
回复
以上程序在VC6.0中要做相应地修改,在DEV-CPP4.9.7.8中无问题.
chinajiji 2003-03-27
  • 打赏
  • 举报
回复
我顺便把<<C++沉思录>>21章节21.4 函数对象模板的那个例子调试了一下,供参考:

//comp.h/////////////////////////////////////

#ifndef COMP_H
#define COMP_H

template <class X, class Y>
class Comp_base {
public:
virtual Y operator()(X) const = 0;
virtual Comp_base* clone() const = 0;
virtual ~Comp_base() {}
};

template<class F, class G, class X, class Y>
class Comp : public Comp_base<X,Y> {
public:
Comp(F f0, G g0) : f(f0), g(g0) {}

Y operator()(X x) const { return f(g(x)); }
Comp_base<X,Y>* clone() const {
return new Comp(*this);
}
private:
F f;
G g;
};

template<class X, class Y>
class Composition {
public:
//declare template member function in a template class
template<class F, class G> Composition(F, G); //?
Composition(const Composition&);
Composition& operator=(const Composition&);
~Composition();
Y operator() (X) const;
private:
Comp_base<X, Y>* p;
};

//class Compositon Complimention:

// define template member function in a template class
template<class X, class Y>
template<class F, class G>
Composition<X,Y>::Composition(F f, G g) :
p(new Comp<F, G, X, Y> (f, g)) {}

template<class X, class Y>
Composition<X, Y>::~Composition() {
delete p;
}

template<class X, class Y>
Composition<X, Y>::Composition(const Composition &c) :
p(c.p->clone()) {}

template<class X, class Y>
Composition<X, Y>& Composition<X, Y>::operator=(const Composition &c) {
if(this != &c) {
delete p;
p = c.p->clone();
}
return *this;
}

template<class X, class Y>
Y Composition<X, Y>::operator() (X x) const {
return (*p)(x); // p->operator()(x);
}

#endif

/////////compFun.cpp//////////////////////
// funtions define:

int f(int i) {
return i * 2;
}

int g(int i) {
return i - 1;
}

int h(int i) {
return i / 2;
}
///////main.cpp ////////////////////////////////////
#include "comp.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

extern int f(int);
extern int g(int);
extern int h(int);

int main(int argc, char *argv[])
{
Composition<int, int> fg(f, g);
Composition<int, int> fgh(fg, h);
cout << "test fg(42):" << endl;
cout << "fg(42) = " << fg(42) << endl;
cout << "test fgh(11):" << endl;
cout << "fgh(11) = " << fgh(11) << endl;
system("PAUSE");
return 0;
}
/*运行结果:
test fg(42):
fg(42) = 82
test fgh(11):
fgh(11) = 8
请按任意键继续 . . .
*/
chinajiji 2003-03-27
  • 打赏
  • 举报
回复
小笨,你的第一种方法在DEV-CPP中可以的,VC6.0中不可以:

#include <iostream>
#include <stdlib.h>
using namespace std;

template <typename X>
class C
{
public:
template <typename Y> void f(Y* p);
private:
int m_m;
};

template <typename X>
template<typename Y>
void C<X>::f(Y* p){
cout << "in f()! " << "*p = " << *p << endl;
}

int main() {
C<int> c;
char ch = 'A';
char* pch = &ch;
c.f(pch);
system("pause");
return 0;
}

VC6.0中这样做:
#include <iostream>
#include <stdlib.h>
using namespace std;

template <typename X>
class C
{
public:
template <typename Y> void f(Y* p) { //in VC6.0
cout << "in f()! " << "*p = " << *p << endl;
}
private:
int m_m;
};

/*
template <typename X>
template<typename Y>
void C<X>::f(Y* p){
cout << "in f()! " << "*p = " << *p << endl;
}
*/

int main() {
C<int> c;
char ch = 'A';
char* pch = &ch;
c.f(pch);
system("pause");
return 0;
}
/*运行结果:

in f()! *p = A
请按任意键继续 . . .
*/
a_hui123 2003-03-27
  • 打赏
  • 举报
回复
micropentium6(小笨) 说的对,但总觉得怪怪的。我明天给分。
woxihuanbohe 2003-03-27
  • 打赏
  • 举报
回复
去参照一下the c++ standard template library!
讲得很详细的
  • 打赏
  • 举报
回复
这样是可以的,但已经脱离你的初衷了
template <typename X,typename Y>
class C
{
public:
// template <typename Y>
void f(Y* p);
private:
int m_m;
};
template < typename X ,typename Y>
// template < typename Y >
void C<X,Y>::f(Y* p){
return;
}
cenlmmx 2003-03-27
  • 打赏
  • 举报
回复
还真没遇到过.
  • 打赏
  • 举报
回复
template <typename X>
template<typename Y>
void C<X>::f(Y* p){
//...
}
cenlmmx 2003-03-27
  • 打赏
  • 举报
回复
没对
cenlmmx 2003-03-27
  • 打赏
  • 举报
回复
template <typename Y>
void C::f(Y* p)
{
....
}

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧