模板和专业化错误 [英] Templates and Specialisation error
问题描述
我遇到模板和专业化问题。我正在使用它来为
重载相同的功能,因此它可以返回不同的东西。虽然我的编译器答应我,但我看不出我错在哪了。下面是我的代码的一个例子,我已经包含了VC给我的错误,如果那个
有帮助。谁能指出我正确的方向?
干杯,
西蒙; o)
-
模板< A级,B级>
A foo(B x);
模板<>
int foo (int x){
返回x + 1;
}
模板<>
float foo(int x){
return(float)(x)+ 1.0f;
}
int main(){
int a = 1;
int b = foo< int,int>(a);
float c = foo< ; float,int>(a);
返回1;
}
VC6正在给...
c:\ test\main.cpp(13):错误C2556:''float __cdecl foo(int)'':重载
函数的区别仅在于返回输入''int __cdecl foo(int)''
c:\ test\main.cpp(8):看'foo'的声明'
c: \test\main.cpp(13):错误C2371:''foo'':重新定义;不同的基本
类型
c:\ test\main.cpp(8):看'foo'的声明'
c:\\ \\ test\main.cpp(21):错误C2893:无法专门化功能模板
''A __cdecl foo(B)''
使用以下模板参数:
''int''
''int''
执行cl.exe时出错。
Hi,
I''m having a problem with templates and specialisation. I''m using it to
overload the same function so it can return different things. I can''t see
what I''m doing wrong, although my compiler promises me I am! Here follows an
example of my code, and i''ve included the error VC is giving me if that
helps. Can anyone point me in the right direction?
Cheers,
Simon ;o)
--
template <class A, class B>
A foo(B x);
template <>
int foo(int x) {
return x + 1;
}
template <>
float foo(int x) {
return (float)(x) + 1.0f;
}
int main () {
int a = 1;
int b = foo<int,int>(a);
float c = foo<float,int>(a);
return 1;
}
VC6 is giving...
c:\test\main.cpp(13) : error C2556: ''float __cdecl foo(int)'' : overloaded
function differs only by return type from ''int __cdecl foo(int)''
c:\test\main.cpp(8) : see declaration of ''foo''
c:\test\main.cpp(13) : error C2371: ''foo'' : redefinition; different basic
types
c:\test\main.cpp(8) : see declaration of ''foo''
c:\test\main.cpp(21) : error C2893: Failed to specialize function template
''A __cdecl foo(B)''
With the following template arguments:
''int''
''int''
Error executing cl.exe.
推荐答案
" Simon" <所以********* @ no.no>在消息中写道
news:bk ********** @ titan.btinternet.com ...
"Simon" <so*********@no.no> wrote in message
news:bk**********@titan.btinternet.com...
>我遇到了模板和专业化的问题。我正在使用它来重载相同的功能,以便它可以返回不同的东西。虽然我的编译器答应我,但我看不出我做错了什么!以下是
我的代码示例,我已经包含了VC给我的错误,如果这有帮助的话。任何人都可以指出我正确的方向吗?
干杯,
西蒙; o)
-
模板< A级,B级>
A foo(B x);
模板<>
int foo(int x){
返回x + 1;
}
模板<>
浮动foo(int x){
哎呀!你不能单独重载返回类型。
见下文。
返回(浮动)(x)+ 1.0f;
}
> int main(){
int a = 1;
int b = foo< int,int>(a);
float c = foo< float,int>(a); <回复1;
VC6正在给予......
c:\ test \ main.cpp(13):错误C2556: ''float __cdecl foo(int)'':重载
函数的区别仅在于''int __cdecl foo(int)''的返回类型'
这是一个准确的诊断。 Functinos不能仅仅在返回类型上重载
。参数类型
和/或计数必须不同。
c:\ test\main.cpp(8):参见''foo''的声明> c:\ test\main.cpp(13):错误C2371:''foo'':重新定义;不同的基本类型
您尝试使用不同的返回类型定义一个超过
的函数。不允许。
参数列表必须不同。返回类型确实
不会影响重载。
c:\ test\main.cpp(8):看'foo'的声明'
c: \test\main.cpp(21):错误C2893:无法专门化功能模板
''__cdecl foo(B)''
使用以下模板参数:
'' int''
''int''
执行cl.exe时出错。
Hi,
I''m having a problem with templates and specialisation. I''m using it to
overload the same function so it can return different things. I can''t see
what I''m doing wrong, although my compiler promises me I am! Here follows an example of my code, and i''ve included the error VC is giving me if that
helps. Can anyone point me in the right direction?
Cheers,
Simon ;o)
--
template <class A, class B>
A foo(B x);
template <>
int foo(int x) {
return x + 1;
}
template <>
float foo(int x) {
Whoops! You cannot overload on return type alone.
See below.
return (float)(x) + 1.0f;
}
int main () {
int a = 1;
int b = foo<int,int>(a);
float c = foo<float,int>(a);
return 1;
}
VC6 is giving...
c:\test\main.cpp(13) : error C2556: ''float __cdecl foo(int)'' : overloaded
function differs only by return type from ''int __cdecl foo(int)''
This is an accurate diagnosis. Functinos cannot be
overloaded on return type only. The parameter types
and/or count must differ.
c:\test\main.cpp(8) : see declaration of ''foo''
c:\test\main.cpp(13) : error C2371: ''foo'' : redefinition; different basic
types
You''re trying to define a function more than
once, with a different return type. Not allowed.
The parameter list must differ. Return type does
not affect overloading.
c:\test\main.cpp(8) : see declaration of ''foo''
c:\test\main.cpp(21) : error C2893: Failed to specialize function template
''A __cdecl foo(B)''
With the following template arguments:
''int''
''int''
Error executing cl.exe.
-Mike
-Mike
Simon写道:
我遇到了模板和专业化的问题。我正在使用它来重载相同的功能,以便它可以返回不同的东西。虽然我的编译器答应我,但我看不出我做错了什么!下面是我的代码示例,我已经包含了VC给我的错误,如果这有帮助的话。任何人都可以指出我正确的方向吗?
干杯,
西蒙; o)
Hi,
I''m having a problem with templates and specialisation. I''m using it to
overload the same function so it can return different things. I can''t see
what I''m doing wrong, although my compiler promises me I am! Here follows an
example of my code, and i''ve included the error VC is giving me if that
helps. Can anyone point me in the right direction?
Cheers,
Simon ;o)
VC6是一个有很多问题的年份模板。
获取VC7.1
VC6 is of a vintage that has alot of problems with templates.
Get VC7.1
Gianni Mariani写道:
Gianni Mariani wrote:
Simon写道:
我遇到了模板和专业化的问题。我正在使用它来重载相同的功能,以便它可以返回不同的东西。我不知道我做错了什么,虽然我的编译器答应我,我是
!下面是我的代码示例,我已经包含了错误
如果有帮助,VC会给我。任何人都可以指出我正确的方向吗?
干杯,
西蒙; o)
Hi,
I''m having a problem with templates and specialisation. I''m using it
to overload the same function so it can return different things. I
can''t see what I''m doing wrong, although my compiler promises me I
am! Here follows an example of my code, and i''ve included the error
VC is giving me if that helps. Can anyone point me in the right
direction?
Cheers,
Simon ;o)
VC6是一个有很多问题的年份模板。
获取VC7.1
VC6 is of a vintage that has alot of problems with templates.
Get VC7.1
除此之外你能说出在这种情况下它出错的规则是什么?或者
猜猜?
-
WW又名阿提拉
Other than that can you tell what rule does it get wrong in this case? Or
just guessing?
--
WW aka Attila
这篇关于模板和专业化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!