类的成员模版函数怎么调用

茹果伱在 2011-08-12 04:16:07

class Test
{
public:
int a;
double b;
char name[20];
Test()
{
a = 10;
b = 5.5;
strcpy(name, "maliang");
}
template <class T>T Get(int Num);
};

template <class T>T Test::Get(int Num)
{
switch(Num){
case 1:
return a;
case 2:
return b;
case 3:
return name;
}
}

void __fastcall TForm1::Button6Click(TObject *Sender)
{
Test p;
p.Get<int>(1);
}


这样不行 要怎么调用啊
...全文
461 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
茹果伱在 2011-08-13
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 sonicling 的回复:]

一个普通函数不能在多处return多个不同的类型,因为return的类型必须与返回类型一致。

一个模板函数也不能在多处return多个不同类型,因为一旦模板参数确定,返回类型就确定了,而所有return都应该与返回类型一致。

T Get(int n);函数里面所有return都应该是T类型的,如果涉及与T不相容的类型,编译都无法通过。
[/Quote]
我把return name去掉之后就可以正常运行了 double和int不也是不同的类型吗
茹果伱在 2011-08-13
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hemiya 的回复:]

引用 8 楼 maliang799 的回复:

为什么把模板函数的声明和定义写在一起就可以的 分开写就不行呢

你的分开写是不是写在cpp文件里了?
要写在h文件里
[/Quote]

是啊 我把定义部分写在CPP文件了 试了下,写在.h真的行了 为什么呢
hemiya 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 maliang799 的回复:]

为什么把模板函数的声明和定义写在一起就可以的 分开写就不行呢
[/Quote]
你的分开写是不是写在cpp文件里了?
要写在h文件里
SonicLing 2011-08-12
  • 打赏
  • 举报
回复
一个普通函数不能在多处return多个不同的类型,因为return的类型必须与返回类型一致。

一个模板函数也不能在多处return多个不同类型,因为一旦模板参数确定,返回类型就确定了,而所有return都应该与返回类型一致。

T Get(int n);函数里面所有return都应该是T类型的,如果涉及与T不相容的类型,编译都无法通过。
茹果伱在 2011-08-12
  • 打赏
  • 举报
回复
为什么把模板函数的声明和定义写在一起就可以的 分开写就不行呢
茹果伱在 2011-08-12
  • 打赏
  • 举报
回复
我这边怎么报错 说Unresolved external 'int Test::Get<int>(int)'reference from.....
jone7319 2011-08-12
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
class Test
{
public:
int a;
double b;
char name[20];
Test()
{
a = 10;
b = 5.5;
strcpy(name, "maliang");
}
template <class T>T Get(int Num);
};

template <class T>T Test::Get(int Num)
{
switch(Num){
case 1:
return (T)a;
case 2:
return (T)b;
case 3:
return (T)name;
}
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
Test p;
p.Get<int>(1);

}
//---------------------------------------------------------------------------
茹果伱在 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jone7319 的回复:]

你事先要声明类才能调用的
[/Quote]
要怎么改哦
jone7319 2011-08-12
  • 打赏
  • 举报
回复
你事先要声明类才能调用的
jone7319 2011-08-12
  • 打赏
  • 举报
回复
我帮你看了一下,p.Get<int>(1); 编译通过,的确是return name;出错了
茹果伱在 2011-08-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jone7319 的回复:]

p.Get<int>(1); //你调用时实例化为返回整型数据,那个name是char *类型就会出错!
[/Quote]
我想返回name的时候就调用 p.Get<char *>(3) 这样不行吗?
关键是现在p.Get<int>(1); 都编译不通过
jone7319 2011-08-12
  • 打赏
  • 举报
回复
p.Get<int>(1); //你调用时实例化为返回整型数据,那个name是char *类型就会出错!

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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