简体   繁体   English

如何调用此功能模板?

[英]How to call this function template?

struct Message1 {
    int a;
    int b;
};
template<class MSG_T> void RegisterMessageType() {
    MSG_T t;
}

Message1 m;
RegisterMessageType<Message1>();

Error message: 错误信息:

error C2909: 'RegisterMessageType': explicit instantiation of function template 
           requires return type

error C4430: missing type specifier - int assumed. 
               Note: C++ does not support default-int

error C2768: 'RegisterMessageType' : illegal use of explicit template arguments

What is wrong? 怎么了? Thanks. 谢谢。

Sorry, edit again. 抱歉,再次编辑。

if i put 如果我放

Message1 m;
RegisterMessageType<Message1>();

into main function, it (VS2010) can compile it Okay. 转换成主要功能,它(VS2010)可以编译它。 But if i put it outside of main, above compiling error occurred. 但是,如果我把它放在main之外,则会发生上述编译错误。

In C++, expressions (such as calling a function) can only occur inside functions (or inside initialisers of variables). 在C ++中,表达式(例如调用函数)只能出现在函数内部(或变量的初始化程序内部)。 Just putting a call outside of a function is an error. 仅将调用放在函数外部是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM