1

我购买了一个 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;
}
4

1 回答 1

0

这段代码完全是胡说八道:

template <typename ValueType> ValueType* any_cast(Any* operand) : public any_cast(operand){};

它看起来像一个模板函数定义:

template <typename ValueType> ValueType* any_cast(Any* operand)

部分。但是还有一个冒号,它只在构造函数初始化列表中是合法的,然后是public,它只在类声明中是合法的。这两个令牌:根本public:不能像这样一起出现。

这里一定有一些宏黑魔法在 MSVC 下编译并且在 GCC 下不起作用。如果是这样,这就是为什么宏可能是邪恶的一个典型例子。当被滥用时,它们会产生一种新的秘密语言,除了作者之外没有人知道。

除非你能从库的作者那里得到直接的支持,否则你可能会在 GCC 下编译它。

于 2012-11-15T16:07:14.273 回答