类模板里面的问题

ww51xh 2004-12-27 12:03:48
template<class Type>
class MyClass:
{
template<class T>
void DoSomething(T t);

};

DoSomething该怎么来定义啊??
请教。。。
...全文
130 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilovevc 2004-12-27
  • 打赏
  • 举报
回复
vc6可以将模板成员函数直接定义在class内。
ilovevc 2004-12-27
  • 打赏
  • 举报
回复
template<class Type>
template<class T>
void MyClass<Type>::DoSomething(T t)
{

}

应该是如此。不过vc6就别指望了。
sonic1984 2004-12-27
  • 打赏
  • 举报
回复
我想楼主的意思是问 模版类中的模版函数该怎么在外部定义吧

好像要用作用域解析符::
blueskyzsz 2004-12-27
  • 打赏
  • 举报
回复
晕,格式咋变了,呵呵
sonic1984 2004-12-27
  • 打赏
  • 举报
回复
你用内联定义吧
blueskyzsz 2004-12-27
  • 打赏
  • 举报
回复
// 简单写了一下 ^_^

#include <iostream>
#include <string>
using namespace std;

template<class T>
class MyClass
{
public:
MyClass() : m_argumT()
{
}
MyClass(T argumT)
{
m_argumT = argumT;
}

void DoSomething(T argumT1)
{
cout << "argumT: " << m_argumT << endl;
cout << "argumT1: " << argumT1 << endl;
}

protected:
T m_argumT;
};

void main()
{
MyClass<int> testInt(30);
MyClass<char> testCh('d');
MyClass<string> testStr("test");

testInt.DoSomething(50);
testCh.DoSomething('m');
testStr.DoSomething("hello world");
}
fallhunter 2004-12-27
  • 打赏
  • 举报
回复

呵呵
beyondtkl 2004-12-27
  • 打赏
  • 举报
回复
ni yao 做什麼??
ww51xh 2004-12-27
  • 打赏
  • 举报
回复
谢谢各位了,俺知道了
结贴了
blueskyzsz 2004-12-27
  • 打赏
  • 举报
回复
带有模板类型的模板函数vc6只能放在头文件中定义,因为以前的编译器很难定位模板类里模板函数的位置
,所以只能在头文件中定义。如果不带的话没问题.
c++里面有一个新的关键字export 用这个关键字可以把模板函数放到cpp里面,
不过我不清楚那个编译器现在支持,vc6一定不支持的
这么写是可以地^_^

template<class T>
class MyClass
{
public:
MyClass() : m_argumT()
{
}
MyClass(T argumT)
{
m_argumT = argumT;
}

template<class OtherT>
void DoSomething(OtherT argumT1)
{
cout << "argumT: " << m_argumT << endl;
cout << "argumT1: " << argumT1 << endl;
}

protected:
T m_argumT;
};
ww51xh 2004-12-27
  • 打赏
  • 举报
回复
to sonic1984() :
对啊,就是模板里面的模板函数应该怎么定义。
谁能给一个例子。。

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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