我购买了一个 Ogre 插件(粒子宇宙)的源代码,它是(或应该是)用独立于平台的 C++ 编写的。它带有一个编译得很好的 Visual Studio 解决方案,但是因为我的整个项目都是用 G++ 编译的,所以我也想用 G++ 编译那个库。但是由于某种原因,模板声明会在 G++ 中引发错误。这是代码片段。
typedef Ogre::Any Any;
template <typename ValueType> ValueType* any_cast(Any* operand) : public any_cast(operand){};
是否有任何 C++ 大师能够告诉我为什么会出现此错误?(也许不同的语法?)
include/ParticleUniverseAny.h: In function 'ValueType*
ParticleUniverse::any_cast(ParticleUniverse::Any*)':
include/ParticleUniverseAny.h:21:68: error: only constructors take member initializers
include/ParticleUniverseAny.h:21:68: error: expected identifier before 'public'
include/ParticleUniverseAny.h:21:68: error: expected '{' before 'public'
include/ParticleUniverseAny.h: At global scope:
include/ParticleUniverseAny.h:21:68: error: expected unqualified-id before 'public'
我会很高兴并感谢任何帮助!
编辑:any_cast 在 OgreAny.h 中定义
friend ValueType * any_cast(Any *);
template<typename ValueType>
ValueType * any_cast(Any * operand)
{
return operand && operand->getType() == typeid(ValueType)
? &static_cast<Any::holder<ValueType> *>(operand->mContent)->held
: 0;
}
template<typename ValueType>
ValueType * any_cast(Any * operand)
{
return operand && operand->getType() == typeid(ValueType)
? &static_cast<Any::holder<ValueType> *>(operand->mContent)->held
: 0;
}
template<typename ValueType>
const ValueType * any_cast(const Any * operand)
{
return any_cast<ValueType>(const_cast<Any *>(operand));
}
template<typename ValueType>
ValueType any_cast(const Any & operand)
{
const ValueType * result = any_cast<ValueType>(&operand);
if(!result)
{
StringUtil::StrStreamType str;
str << "Bad cast from type '" << operand.getType().name() << "' "
<< "to '" << typeid(ValueType).name() << "'";
OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
str.str(),
"Ogre::any_cast");
}
return *result;
}