inheritance with templates trouble

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

    inheritance with templates trouble

    Sorry if this is an obvious question, but I try to let a template class
    inherit another template class.

    The code here under is a simplification of how I try to do that, but
    the compiler complains that 'c' in line '30 ' wasn't declared.

    " (g++)
    test.h: In member function ‘T Test2<T>::give( )’:
    test.h:30: error: ‘c’ was not declared in this scope
    "

    I have absolute no idea what the problem and/or solution is, can anybody
    help me with that please?

    Thank you,
    Driehoek


    ////////////////////////////////////////
    4: template <class T>
    5: class Test
    6: {
    7: public:
    8: virtual void
    9: set (T i)
    10: {
    11: c = i;
    12:
    13: }
    14: virtual T
    15: give ()
    16: {
    17: return c;
    18: }
    19: protected:
    20: T c;
    21: };
    22:
    23: template <class T>
    24: class Test2 : public Test<T>
    25: {
    26: public:
    27: T
    28: give ()
    29: {
    30: return c * 2;
    31: }
    32: };
    ////////////////////////////////////////
  • richard

    #2
    Re: inheritance with templates trouble

    On Fri, 08 Aug 2008 12:55:21 +0000, richard wrote:

    Finally I brute forced the solution :), I had to change line 30 to:

    ////
    return Test<T>::c * 2;
    ////

    But I don't really see the logic in this, why do you need to specify the
    base class?

    Thanks.

    Comment

    • Stuart Golodetz

      #3
      Re: inheritance with templates trouble

      "richard" <rafeltjerafela ar@dse.nlwrote in message news:ZmXmk.151$ au1.146@read4.i net.fi...
      Sorry if this is an obvious question, but I try to let a template class
      inherit another template class.

      The code here under is a simplification of how I try to do that, but
      the compiler complains that 'c' in line '30 ' wasn't declared.

      " (g++)
      test.h: In member function â?~T Test2<T>::give( )â?T:
      test.h:30: error: â?~câ?T was not declared in this scope
      "

      I have absolute no idea what the problem and/or solution is, can anybody
      help me with that please?

      Thank you,
      Driehoek


      ////////////////////////////////////////
      4: template <class T>
      5: class Test
      6: {
      7: public:
      8: virtual void
      9: set (T i)
      10: {
      11: c = i;
      12:
      13: }
      14: virtual T
      15: give ()
      16: {
      17: return c;
      18: }
      19: protected:
      20: T c;
      21: };
      22:
      23: template <class T>
      24: class Test2 : public Test<T>
      25: {
      26: public:
      27: T
      28: give ()
      29: {
      30: return c * 2;
      31: }
      32: };
      ////////////////////////////////////////
      I think c's a dependent name: you want "this->c" rather than just "c".

      HTH,
      Stu

      Comment

      • Victor Bazarov

        #4
        Re: inheritance with templates trouble

        richard wrote:
        On Fri, 08 Aug 2008 12:55:21 +0000, richard wrote:
        >
        Finally I brute forced the solution :), I had to change line 30 to:
        >
        ////
        return Test<T>::c * 2;
        ////
        >
        But I don't really see the logic in this, why do you need to specify the
        base class?
        Search the FAQ for "dependent names".

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask

        Comment

        • richard

          #5
          Re: inheritance with templates trouble

          On Fri, 08 Aug 2008 09:41:59 -0400, Victor Bazarov wrote:

          Thank you, now it makes sense to me.

          Comment

          Working...