class 中的模板成员
Template members in class
我正在尝试在非模板中包含模板成员 class。这是示例:
.h
class ncScript {
public:
template<typename T> void RegisterConstant( T value, const char *N );
};
.cpp
template<>
void RegisterConstant<int>( int value, const char * N ) {
// Do something.
}
template<>
void RegisterConstant<bool>( bool value, const char * N ) {
// Do something.
}
// and so on
但是当我尝试像这样使用它时:
_luaCache["myluafile"].RegisterConstant( 13, "myvariable" );
我在 Xcode(LLVM 编译器)中收到以下错误:
Explicit specialization of 'RegisterConstant<int>' after instantiation.
错误告诉您 class 在编译器看到特化之前被实例化。
例如,如果它看到这个,class 要么已经实例化,要么将在此处。
_luaCache["myluafile"].RegisterConstant( 13, "myvariable" );
如果稍后实施专业化,您将收到错误消息。
最简单的解决方案是将专业化 声明 放在 header 中,即
template<>
void RegisterConstant<int>( int value, const char * N );
我正在尝试在非模板中包含模板成员 class。这是示例:
.h
class ncScript {
public:
template<typename T> void RegisterConstant( T value, const char *N );
};
.cpp
template<>
void RegisterConstant<int>( int value, const char * N ) {
// Do something.
}
template<>
void RegisterConstant<bool>( bool value, const char * N ) {
// Do something.
}
// and so on
但是当我尝试像这样使用它时:
_luaCache["myluafile"].RegisterConstant( 13, "myvariable" );
我在 Xcode(LLVM 编译器)中收到以下错误:
Explicit specialization of 'RegisterConstant<int>' after instantiation.
错误告诉您 class 在编译器看到特化之前被实例化。
例如,如果它看到这个,class 要么已经实例化,要么将在此处。
_luaCache["myluafile"].RegisterConstant( 13, "myvariable" );
如果稍后实施专业化,您将收到错误消息。
最简单的解决方案是将专业化 声明 放在 header 中,即
template<>
void RegisterConstant<int>( int value, const char * N );