问题:调用模板成员函数 [英] problem: calling a template member function

查看:68
本文介绍了问题:调用模板成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这段代码:


[开始main.cpp]

模板< typename ClassType>

struct S

{

template< typename FunctionType> void member(){};

};


template< typename Type>

void g()

{

S< Type> s;

/ *第11行* / s.member<类型>();

}


void h()

{

S< int> s;

s.member< int>();

}


int main(int,char **)

{

g< int>();

h();

返回0;

}

[end main.cpp]


用g ++(GCC)3.4.2(mingw-special)编译它并得到这个编译器

错误:

... / main.cpp:函数`void g()'':

... / main。 cpp:11:错误:在''>'之前预期的primary-expression''令牌

... / main.cpp:11:错误:''之前的预期primary-expression''令牌


我不明白第11行和

中的表达式有什么问题,其中g和h的调用之间的差异是。


有谁能解释一下这个?


TIA

Gabriel

Hi
I have this code:

[begin main.cpp]
template<typename ClassType>
struct S
{
template<typename FunctionType> void member() {};
};

template<typename Type>
void g()
{
S<Type> s;
/*line 11*/ s.member<Type>();
}

void h()
{
S<int> s;
s.member<int>();
}

int main(int, char**)
{
g<int>();
h();
return 0;
}
[end main.cpp]

I compile it with g++ (GCC) 3.4.2 (mingw-special) and get this compiler
error:
.../main.cpp: In function `void g()'':
.../main.cpp:11: error: expected primary-expression before ''>'' token
.../main.cpp:11: error: expected primary-expression before '')'' token

I do not understand what is wrong with the expression in Line 11 and
where the difference between the calls in g and h is.

Can anyone explain this?

TIA
Gabriel

推荐答案

Gabriel,我接受了你的代码并用MSVC 7.1编译它,其中
增加了与C ++标准的一致性。它编译并运行正常。我知道

MSVC7.1和g ++ 3.4.2是不同的但是不是两个编译器都试图使用符合C ++标准的
?谢谢

Gabriel, I took your code and compiled it with MSVC 7.1 , which
increases conformance with C++ standard. It compiled and ran OK. I know
MSVC7.1 and g++ 3.4.2 are different but don''t both compilers try to
conform with C++ standard? Thank you


Frank Chang写道:
Frank Chang wrote:
Gabriel,我拿了你的代码并用MSVC 7.1编译,其中提高与C ++标准的一致性。它编译并运行正常。我知道
MSVC7.1和g ++ 3.4.2是不同的,但是不是两个编译器都试图符合C ++标准吗?谢谢
Gabriel, I took your code and compiled it with MSVC 7.1 , which
increases conformance with C++ standard. It compiled and ran OK. I know
MSVC7.1 and g++ 3.4.2 are different but don''t both compilers try to
conform with C++ standard? Thank you




谢谢你,弗兰克


是的,我认为他们都试图符合标准。我认为

g ++ 3.4(或更高版本)非常接近标准。据我所知

语言,我的代码应该是正确的,但我不确定g ++

是否应该翻译它,或者MSVC是否通过翻译犯了错误它。


如果它是g ++的问题,有没有人知道解决方法?



Thank you, Frank

Yes, I think that they both try to conform to the standard. And I think
g++ 3.4 (or later) is very close to the standard. As far as I understand
the language, my code should be correct, but I am not sure if the g++
should translate it or if the MSVC makes a mistake by translating it.

If it''s a problem with the g++, does anyone know a workaround?


一些额外的信息:


我刚刚在IAR Embedded Workbench IDE下使用MSP430 IAR

C / C ++编译器运行它,它告诉我


错误[Pe254]:不允许输入类型名称H:\Projects \ Test(IAR Embedded

Workbench IDE)\ main.cpp 11

错误[P​​e029]:预期表达式为H:\Projects \ Test(IAR Embedded

Workbench IDE)\ main.cpp 11

Gabriel
Some additional information:

I just ran it under the IAR Embedded Workbench IDE with the MSP430 IAR
C/C++ Compiler, and it tells me

Error[Pe254]: type name is not allowed H:\Projects\Test (IAR Embedded
Workbench IDE)\main.cpp 11
Error[Pe029]: expected an expression H:\Projects\Test (IAR Embedded
Workbench IDE)\main.cpp 11

Gabriel


这篇关于问题:调用模板成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆