模板代码返回类型重载无法编译。怎么了
我正在尝试使用以下代码来重载一个简单的函数以适用于多种类型。但是,它无法编译。有人可以告诉我出了什么问题以及如何解决这个问题吗?
typedef struct str1_type
{
int f1;
int f2;
} STR1;
typedef struct str2_type
{
char f1[100];
char f2[100];
}STR2;
template <typename T1, typename T2>
T2 getFieldOffset(const T1& t, int i);
int main() {
STR1 str1;
STR2 str2;
int i = getFieldOffset(str1,0);
const char* test = getFieldOffset(str2,0);
}
template <typename T1, typename T2>
T2 getFieldOffset(const T1& t, int i)
{
switch (i) {
case 0:
return t.f1;
case 1:
return t.f2;
default:
{
cout << "Invalid index passed: i" << i << endl;
return null;
}
}
}
这是错误消息:
test2.cpp:在函数“int main()”中:
test2.cpp:73: 错误:没有匹配的函数可用于调用“getFieldOffset(STR1&, int)”
test2.cpp:75: 错误:没有匹配的函数可用于调用“getFieldOffset(STR2&, int)”test2.cpp:在函数“T2 getFieldOffset(const T1&, int)”中:
test2.cpp:90: 错误: 'null' 未在此范围内声明
I am trying the following code to overload a simple function to work for multiple types. However, it fails to compile. Can some one please tell me whats wrong and how to fix this?
typedef struct str1_type
{
int f1;
int f2;
} STR1;
typedef struct str2_type
{
char f1[100];
char f2[100];
}STR2;
template <typename T1, typename T2>
T2 getFieldOffset(const T1& t, int i);
int main() {
STR1 str1;
STR2 str2;
int i = getFieldOffset(str1,0);
const char* test = getFieldOffset(str2,0);
}
template <typename T1, typename T2>
T2 getFieldOffset(const T1& t, int i)
{
switch (i) {
case 0:
return t.f1;
case 1:
return t.f2;
default:
{
cout << "Invalid index passed: i" << i << endl;
return null;
}
}
}
Here is the error message:
test2.cpp: In function 'int main()':
test2.cpp:73: error: no matching function for call to 'getFieldOffset(STR1&, int)'
test2.cpp:75: error: no matching function for call to 'getFieldOffset(STR2&, int)'test2.cpp: In function 'T2 getFieldOffset(const T1&, int)':
test2.cpp:90: error: 'null' was not declared in this scope
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只能从参数推导出模板参数
T1
,因此在调用时必须在模板参数列表中显式提供模板参数T2
。但是,如果您使用编写的函数模板,则必须为两个模板形参提供实参。您应该交换模板参数列表中
T1
和T2
的位置,这样您只需指定T2
:或者,使用更好的名称模板参数:
现在您可以将其称为:
Only template argument
T1
can be deduced from the arguments, so you have to explicitly provide the template argumentT2
in the template argument list when you make the call.However, if you use the function template as written, you have to provide arguments for both template parameters. You should swap the positions of
T1
andT2
in the template parameter list so that you only have to specifyT2
:Or, with better names for the template parameters:
Now you can call this as:
编译器无法推断返回类型。原因之一是
getFIeldOffset
可能返回一个不是int
但可转换为int
的值。您必须指定何时返回类型为:The compiler cannot deduce return types. One reason is because
getFIeldOffset
might return a value that isn't anint
, but is convertible toint
. You must specify when the return type is:看起来你应该写 NULL 而不是 null 并且所有这些都会正常工作。
问题在于模板实例化,编译器由于不正确的“null”而无法实例化模板,因此他不考虑使用模板作为函数调用的候选者的能力。
Looks like you should write NULL instead of null and all of it will work fine.
The problem is in template instantiation, compiler could not instantiate template because of incorrect 'null', so he doesn't look at ability to use your template as candidate for your function calling.
getFieldOffset
是一个函数模板,它采用两个类型参数。但在上面的代码中,您没有提供类型参数。您希望编译器推断它们。但编译器只能推断出函数参数类型,而不能推断出返回类型。因此出现了错误。您必须提供两个类型参数:
但是,如果您反转函数模板中类型的顺序为,
那么在调用函数时您只能提供一种类型,即返回类型;另一种类型可以从函数参数推导出来:
getFieldOffset
is a function template which takes two type argument. But in the above code, you didn't provide type argument. You want the compiler to deduce them. But the compiler can deduce only the function parameter type, not the return type. Hence the error.You've to provide both type arguments:
However if you reverse the order of types in the function template as
Then you can provide only one type, the return type, when calling the function; the other type can be deduced from the function argument: