C ++ - 具有类函数引用的模板无法在Android上编译

时间:2015-12-22 02:30:33

标签: android c++ ios android-ndk cocos2d-x

我目前正在尝试使用某种特殊模板。这是一个非常简单的例子,可以准确显示出现了什么问题。 这是模板:

template <typename ClassT,  int (ClassT::*Func)() const>
class TEST
{
public:
    TEST(ClassT const * selfVar) : _this(selfVar) {
    }

private:
    ClassT const* _this;
    };

此代码是我项目的Entity.h文件:

int _a;
int getA() const {
    return _a;
}

TEST<TestClass, &TestClass::getA> test = TEST<TestClass, &TestClass::getA>(this);

使用Xcode为iOS编译代码很好但不适用于Android,但有以下错误。在构造函数中进行初始化是没有选择的 - 我知道这样做有效,但我需要将它作为.h文件中的单行。

这是错误日志:

jni/../../Classes/Entity.h:37:54: warning: extra qualification 'Entity::' on member 'getA' [-fpermissive]
 TEST<Entity, &Entity::getA> yoyo = TEST<Entity, &Entity::getA>();
                                                  ^
jni/../../Classes/Entity.h:37:62: error: expected ';' at end of member declaration
 TEST<Entity, &Entity::getA> yoyo = TEST<Entity, &Entity::getA>();
                                                          ^
jni/../../Classes/Entity.h:37:62: error: 'TEST<Entity, &Entity::getA>& Entity::getA' conflicts with a previous declaration
jni/../../Classes/Entity.h:29:9: note: previous declaration 'int Entity::getA() const'
 int getA() const {
     ^
jni/../../Classes/Entity.h:37:66: error: expected unqualified-id before '>' token
 TEST<Entity, &Entity::getA> yoyo = TEST<Entity, &Entity::getA>();
                                                              ^
jni/../../Classes/Entity.h:37:45: error: wrong number of template arguments (1, should be 2)
 TEST<Entity, &Entity::getA> yoyo = TEST<Entity, &Entity::getA>();
                                         ^

1 个答案:

答案 0 :(得分:0)

template <typename ClassT,  int (ClassT::*Func)() const>
class TEST
{
public:
    TEST(ClassT const * selfVar) : _this(selfVar) {
    }

private:
    ClassT const* _this;
};

struct Entity {
    int _a;
    int getA() const {
        return _a;
    }
};

class TestClass: public Entity {
    void method() {
        TEST<Entity, &Entity::getA> test(this);
    }
};