Invoking templatized base class constructor from templatized derived class constructor

Discussion in 'C++' started by mrstephengross, May 18, 2005.

  1. Ok, I've got code that looks something like this:

    ==================================================

    template<typename T1, typename T2>
    class Base
    {
    public:
    explicit Base(const T1 & t1) { /* ... */ }
    };

    template<typename T1, typename T2, typename T3>
    class Derived : public Base<T1, T2>
    {
    public:
    Derived() : Base(my_t3) { /* ... */ }

    private:
    T3 my_t3;
    };

    ====================================================

    GCC 3.3.1 reports the following error:

    In constructor `Derived<T1, T2, T3>::Derived()':
    error: class `Derived<T1, T2, T3>' does not have any field named
    `template<class T1, T2> class Base'

    It would seem that GCC interprets my initialization of Base in
    Derived's constructor as a field assignment. Is my syntax wrong? Is
    there some way to more directly indicate that I'm invoking Base's
    constructor?

    Thanks,
    --Steve ()
     
    mrstephengross, May 18, 2005
    #1
    1. Advertising

  2. Re: Invoking templatized base class constructor from templatizedderived class constructor

    mrstephengross wrote:
    > Ok, I've got code that looks something like this:
    >
    > ==================================================
    >
    > template<typename T1, typename T2>
    > class Base
    > {
    > public:
    > explicit Base(const T1 & t1) { /* ... */ }
    > };
    >
    > template<typename T1, typename T2, typename T3>
    > class Derived : public Base<T1, T2>
    > {
    > public:
    > Derived() : Base(my_t3) { /* ... */ }


    Has to be

    Derived() : Base<T1,T2>(my_t3) { }

    or you need to typedef 'Base<T1,T2>' as "Base" before.

    >
    > private:
    > T3 my_t3;
    > };
    >
    > ====================================================
    >
    > GCC 3.3.1 reports the following error:
    >
    > In constructor `Derived<T1, T2, T3>::Derived()':
    > error: class `Derived<T1, T2, T3>' does not have any field named
    > `template<class T1, T2> class Base'
    >
    > It would seem that GCC interprets my initialization of Base in
    > Derived's constructor as a field assignment. Is my syntax wrong?


    Yes.

    > Is
    > there some way to more directly indicate that I'm invoking Base's
    > constructor?


    See above.

    V
     
    Victor Bazarov, May 18, 2005
    #2
    1. Advertising

  3. Thanks!

    --Steve
     
    mrstephengross, May 18, 2005
    #3
  4. Re: Invoking templatized base class constructor from templatizedderived class constructor

    Victor Bazarov wrote:
    > mrstephengross wrote:
    >> Ok, I've got code that looks something like this:
    >>
    >> ==================================================
    >>
    >> template<typename T1, typename T2>
    >> class Base
    >> {
    >> public:
    >> explicit Base(const T1 & t1) { /* ... */ }
    >> };
    >>
    >> template<typename T1, typename T2, typename T3>
    >> class Derived : public Base<T1, T2>
    >> {
    >> public:
    >> Derived() : Base(my_t3) { /* ... */ }

    >
    > Has to be
    >
    > Derived() : Base<T1,T2>(my_t3) { }


    That's the correct syntax, but isn't there a logic flaw
    in the Base() call? The Base() constructor expects a
    'T1' parameter, rather than a 'T3' parameter.

    [snip]

    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
     
    Larry I Smith, May 18, 2005
    #4
  5. >That's the correct syntax, but isn't there a logic flaw
    in the Base() call? The Base() constructor expects a
    'T1' parameter, rather than a 'T3' parameter.

    Yeah, you're right... I didn't paste in my example correctly. But the
    main issue has been resolved: apparently C++ requires that you
    explicitly specify the constructor's template arguments. I had been
    using a compiler (KCC) that was more forgiving...

    --Steve
     
    mrstephengross, May 18, 2005
    #5
  6. Re: Invoking templatized base class constructor from templatizedderived class constructor

    mrstephengross wrote:
    >>That's the correct syntax, but isn't there a logic flaw

    > in the Base() call? The Base() constructor expects a
    > 'T1' parameter, rather than a 'T3' parameter.
    >
    > Yeah, you're right... I didn't paste in my example correctly. But the
    > main issue has been resolved: apparently C++ requires that you
    > explicitly specify the constructor's template arguments. I had been
    > using a compiler (KCC) that was more forgiving...
    >
    > --Steve
    >


    "Base<T1, T2>" IS the class name. "Base" is something
    entirely different. g++ is merely enforcing the standard.

    BTW, you do know that you compile/link C++ code with the GCC
    command "g++", NOT with the command "gcc"???

    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
     
    Larry I Smith, May 18, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ali
    Replies:
    4
    Views:
    629
    David Harmon
    Mar 5, 2007
  2. Replies:
    1
    Views:
    425
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    413
    Victor Bazarov
    May 23, 2007
  4. Rahul
    Replies:
    16
    Views:
    1,253
    terminator
    Nov 7, 2007
  5. Replies:
    2
    Views:
    751
Loading...

Share This Page