bzhu
bzhu

Reputation: 954

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. But if i put it outside of main, above compiling error occurred.

Upvotes: 0

Views: 204

Answers (1)

In C++, expressions (such as calling a function) can only occur inside functions (or inside initialisers of variables). Just putting a call outside of a function is an error.

Upvotes: 4

Related Questions