Template inheritance comile problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Helge

    Template inheritance comile problem

    I have a prbl. cannot seem to compile this simple code: (might this be a
    bug?)

    gcc version 4.1.2

    //Begin code

    template <class T>
    class FirstClass{
    public:
    T a;
    };

    template <class T>
    class SecondClass : public FirstClass<T>{
    public:
    T func(){return a;}
    };

    int main(){}

    //End Code

    Compiler

    test.cpp: In member function ‘T SecondClass<T>: :func()’:
    test.cpp:10: error: ‘a’ was not declared in this scope
  • Gianni Mariani

    #2
    Re: Template inheritance comile problem

    Helge wrote:
    I have a prbl. cannot seem to compile this simple code: (might this be a
    bug?)
    >
    gcc version 4.1.2
    >
    //Begin code
    >
    template <class T>
    class FirstClass{
    public:
    T a;
    };
    >
    template <class T>
    class SecondClass : public FirstClass<T>{
    public:
    T func(){return a;}
    try this line:
    T func(){return this->a;}
    };
    >
    int main(){}
    >
    //End Code
    >
    Compiler
    >
    test.cpp: In member function ‘T SecondClass<T>: :func()’:
    test.cpp:10: error: ‘a’ was not declared in this scope

    Comment

    • John Harrison

      #3
      Re: Template inheritance comile problem

      Helge wrote:
      I have a prbl. cannot seem to compile this simple code: (might this be a
      bug?)
      >
      gcc version 4.1.2
      >
      //Begin code
      >
      template <class T>
      class FirstClass{
      public:
      T a;
      };
      >
      template <class T>
      class SecondClass : public FirstClass<T>{
      public:
      T func(){return a;}
      };
      >
      int main(){}
      >
      //End Code
      >
      Compiler
      >
      test.cpp: In member function ‘T SecondClass<T>: :func()’:
      test.cpp:10: error: ‘a’ was not declared in this scope
      Gianni told you the answer, but no it isn't a bug. It's 'two phase
      lookup', a complicated subject. Try googling if you want more enlightenment.

      john

      Comment

      • Grizlyk

        #4
        Re: Template inheritance comile problem

        John Harrison wrote:
        >>
        >template <class T>
        >class SecondClass : public FirstClass<T>{
        using FirstClass<T>:: a;
        >public:
        > T func(){return a;}
        >};
        >
        Gianni told you the answer, but no it isn't a bug. It's 'two phase
        lookup', a complicated subject. Try googling if you want more
        enlightenment.


        --
        Maksim A. Polyanin


        "In thi world of fairy tales rolls are liked olso"
        /Gnume/


        Comment

        Working...