嵌套模板gcc编译器4.1.2错误

Nested templates gcc compiler 4.1.2 error

本文关键字:错误 编译器 gcc 嵌套      更新时间:2023-10-16

我正在尝试创建一个模板类来将用户与数据类型隔离开来。我更喜欢使用适配器类,但需要更改的函数签名需要一个模板。

在下面的代码示例中(不是实际的项目,只是说明问题的简化版本),而在主例程中,我可以使用ob_traits接口。但是,当我尝试创建使用ob_traits作为基类的模板化StructWrapper时,我会遇到错误,gcc无法识别创建的IntAdapter类。这在MSVC 8.0上编译,但在gcc 4.1.2 20070626(Red hat 4.1.2-14)上失败

因此,首先有两个问题,你明白为什么编译失败并出现下面指定的错误吗?

第二,关于如何以更简单的方式实施这一概念,有什么建议吗?

    #include <iostream>
    template <typename T >
    struct ob_traits
    {
        ob_traits( T& param ) { value = param; };
        T value;
    };
    struct GeneralStructure
    {
        int a;
        GeneralStructure(int param):a(param){}
    };
    struct DifferentStructure
    {
        GeneralStructure hidden;
        DifferentStructure( int param ):hidden(param){};
    }
    ;
    /*template< typename T > struct ob_traits
    {
    };
    */
    template<> struct ob_traits< GeneralStructure >
    {
        struct IntAdapter
        {
            IntAdapter( GeneralStructure& valueParam ):value(valueParam){}
            GeneralStructure value;
            int& getValue() { return value.a; };
        };
    };
    template<> struct ob_traits< DifferentStructure >
    {
        struct IntAdapter
        {
            IntAdapter( DifferentStructure& valueParam):value( valueParam ){}
            DifferentStructure value;
            int& getValue( ){ return value.hidden.a; };
        };
        void dump()
        {
            DifferentStructure testLocal(44);
            IntAdapter local( testLocal );
            std::cout << local.getValue()<<std::endl;
        }
    };
    template <typename T > struct StructWrapper:public ob_traits< T >
    {
        StructWrapper(){};
    /*main.cpp:60: error: 'IntAdapter' was not declared in this scope
    main.cpp:60: error: expected `;' before 'inner'
    main.cpp:60: error: 'inner' was not declared in this scope
    */
        void dumpOuter(const T& tempParam) { IntAdapter inner(tempParam); inner.dump(); };
    /*
    main.cpp: In member function 'void StructWrapper<T>::dumpOuterFailsAsWell(const T&)':
    main.cpp:66: error: expected `;' before 'inner'
    main.cpp:66: error: 'inner' was not declared in this scope
    */
        void dumpOuterFailsAsWell(const T& tempParam) { ob_traits<T>::IntAdapter inner(tempParam); inner.dump(); };
    };
    int main(int argc, char* argv[])
    {
        GeneralStructure dummyGeneral(22);
        ob_traits<struct GeneralStructure >::IntAdapter test(dummyGeneral);
        DifferentStructure dummyDifferent(33);
        ob_traits<struct DifferentStructure >::IntAdapter test2(dummyDifferent);
        std::cout << "GeneralStructure: "<<test.getValue()<<std::endl;
        std::cout << "DifferentStructure: "<<test2.getValue()<<std::endl;
        ob_traits<struct DifferentStructure > test3;
        test3.dump();
    std::cout << "Test Templatedn";
    return 0;
    }

dumpOuter失败,因为IntAdapter需要合格(如参考问题中所述)。dumpOuterFailsAsWell失败是因为GCC解析这个代码,尽管它还不完整,所以它需要知道它是一个你所说的类型:

void dumpOuterWorks(const T& tempParam) 
{ 
   typename ob_traits<T>::IntAdapter inner(tempParam); 
   inner.dump(); 
}

如果没有typename,GCC将假定IntAdapter是一个标识符,并期望您形成一个表达式而不是变量声明。

还要注意,您不必在方法体后面放分号!

StructWrapper继承自主类模板(即最不专业的),该模板没有定义IntWrapper,因此不能在此类中使用。我不确定用更专业的类型之一实例化StructWrapper是否能让它工作,或者它在编译类定义本身时是否失败。

编译失败,因为IntAdapter只出现在专用模板中,因此在引用点不可见。

不清楚你会用它做什么?请澄清情况。