how to call an inherited, template class constructor from initializerlist of an inheriting, non-temp

Discussion in 'C++' started by l.s.rockfan@web.de, Nov 14, 2008.

  1. Guest

    Hello,

    how do i have to call an inherited, templated class constructor from the
    initializer list of the inheriting, non-templated class constructor?

    example code:


    template<typename T>
    class A
    {
    protected:
    sometype* something;
    T something_else; /*gives the template some sense here*/
    public:
    A(sometype* param) : something(param) {};
    }


    class B : public A<int>
    {
    public:
    B(sometype* param) : A(param) {}; // <== Compiler Error

    /* further member functions */
    }


    The compiler always tries to identify A as a member variable not being
    found, instead of the base class' constructor.
    , Nov 14, 2008
    #1
    1. Advertising

  2. Salt_Peter Guest

    Re: how to call an inherited, template class constructor frominitializer list of an inheriting, non-template class constructor

    On Nov 14, 3:30 pm, "" <> wrote:
    > Hello,
    >
    > how do i have to call an inherited, templated class constructor from the
    > initializer list of the inheriting, non-templated class constructor?
    >
    > example code:
    >
    > template<typename T>
    > class A
    > {
    > protected:
    >         sometype* something;
    >         T something_else;       /*gives the template some sense here*/
    > public:
    >         A(sometype* param) : something(param) {};
    >
    > }
    >
    > class B : public A<int>
    > {
    > public:
    >         B(sometype* param) : A(param) {}; // <== Compiler Error
    >
    >         /* further member functions */
    >
    > }
    >
    > The compiler always tries to identify A as a member variable not being
    > found, instead of the base class' constructor.


    The following is a class:

    class A { };

    this is not:

    class A { }

    The following declares a class and defines a constructor:

    class A
    {
    A() { }
    };

    or

    // A.hpp (missing include guards)
    class A
    {
    A(); // declaration only
    };

    // A.cpp
    A::A() { } // definition

    Basically, a semicolon denotes a declaration.
    Salt_Peter, Nov 14, 2008
    #2
    1. Advertising

  3. Guest

    Re: how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor

    Salt_Peter wrote:
    > On Nov 14, 3:30 pm, "" <> wrote:
    >> Hello,
    >>
    >> how do i have to call an inherited, templated class constructor from the
    >> initializer list of the inheriting, non-templated class constructor?
    >>
    >> example code:
    >>
    >> template<typename T>
    >> class A
    >> {
    >> protected:
    >> sometype* something;
    >> T something_else; /*gives the template some sense here*/
    >> public:
    >> A(sometype* param) : something(param) {};
    >>
    >> }
    >>
    >> class B : public A<int>
    >> {
    >> public:
    >> B(sometype* param) : A(param) {}; // <== Compiler Error
    >>
    >> /* further member functions */
    >>
    >> }
    >>
    >> The compiler always tries to identify A as a member variable not being
    >> found, instead of the base class' constructor.

    >
    > The following is a class:
    >
    > class A { };
    >
    > this is not:
    >
    > class A { }
    >
    > The following declares a class and defines a constructor:
    >
    > class A
    > {
    > A() { }
    > };
    >
    > or
    >
    > // A.hpp (missing include guards)
    > class A
    > {
    > A(); // declaration only
    > };
    >
    > // A.cpp
    > A::A() { } // definition
    >
    > Basically, a semicolon denotes a declaration.


    That's not the point. I just forgot the semicolons in the example.

    I found out, that my problem is not only specific to explicit
    constructor calls, but occurs everytime I want to call a polymorph
    member function of the base class (which is a template class).

    I get an undefined reference error from ld.

    That's why I start a new post for the more general problem description.
    , Nov 15, 2008
    #3
  4. Guest

    SOLVED: how to call an inherited, template class constructor frominitializer list of an inheriting, non-template class constructor

    wrote:
    > Salt_Peter wrote:
    >> On Nov 14, 3:30 pm, "" <> wrote:
    >>> Hello,
    >>>
    >>> how do i have to call an inherited, templated class constructor from the
    >>> initializer list of the inheriting, non-templated class constructor?
    >>>
    >>> example code:
    >>>
    >>> template<typename T>
    >>> class A
    >>> {
    >>> protected:
    >>> sometype* something;
    >>> T something_else; /*gives the template some sense here*/
    >>> public:
    >>> A(sometype* param) : something(param) {};
    >>>
    >>> }
    >>>
    >>> class B : public A<int>
    >>> {
    >>> public:
    >>> B(sometype* param) : A(param) {}; // <== Compiler Error
    >>>
    >>> /* further member functions */
    >>>
    >>> }
    >>>
    >>> The compiler always tries to identify A as a member variable not being
    >>> found, instead of the base class' constructor.

    >>
    >> The following is a class:
    >>
    >> class A { };
    >>
    >> this is not:
    >>
    >> class A { }
    >>
    >> The following declares a class and defines a constructor:
    >>
    >> class A
    >> {
    >> A() { }
    >> };
    >>
    >> or
    >>
    >> // A.hpp (missing include guards)
    >> class A
    >> {
    >> A(); // declaration only
    >> };
    >>
    >> // A.cpp
    >> A::A() { } // definition
    >>
    >> Basically, a semicolon denotes a declaration.

    >
    > That's not the point. I just forgot the semicolons in the example.
    >
    > I found out, that my problem is not only specific to explicit
    > constructor calls, but occurs everytime I want to call a polymorph
    > member function of the base class (which is a template class).
    >
    > I get an undefined reference error from ld.
    > That's why I start a new post for the more general problem description.


    Okay, the problem was that I have used neither the import nor the export
    model for template source code organization[1].

    I chose the import model and moved the definitions of the template class
    member funtions into the header file and everything works.

    [1] http://www.ddj.com/cpp/184401563
    , Nov 15, 2008
    #4
  5. Guest

    SOLVED: how to call an inherited, template class constructor frominitializer list of an inheriting, non-template class constructor

    wrote:
    > Salt_Peter wrote:
    >> On Nov 14, 3:30 pm, "" <> wrote:
    >>> Hello,
    >>>
    >>> how do i have to call an inherited, templated class constructor from the
    >>> initializer list of the inheriting, non-templated class constructor?
    >>>
    >>> example code:
    >>>
    >>> template<typename T>
    >>> class A
    >>> {
    >>> protected:
    >>> sometype* something;
    >>> T something_else; /*gives the template some sense here*/
    >>> public:
    >>> A(sometype* param) : something(param) {};
    >>>
    >>> }
    >>>
    >>> class B : public A<int>
    >>> {
    >>> public:
    >>> B(sometype* param) : A(param) {}; // <== Compiler Error
    >>>
    >>> /* further member functions */
    >>>
    >>> }
    >>>
    >>> The compiler always tries to identify A as a member variable not being
    >>> found, instead of the base class' constructor.

    >>
    >> The following is a class:
    >>
    >> class A { };
    >>
    >> this is not:
    >>
    >> class A { }
    >>
    >> The following declares a class and defines a constructor:
    >>
    >> class A
    >> {
    >> A() { }
    >> };
    >>
    >> or
    >>
    >> // A.hpp (missing include guards)
    >> class A
    >> {
    >> A(); // declaration only
    >> };
    >>
    >> // A.cpp
    >> A::A() { } // definition
    >>
    >> Basically, a semicolon denotes a declaration.

    >
    > That's not the point. I just forgot the semicolons in the example.
    >
    > I found out, that my problem is not only specific to explicit
    > constructor calls, but occurs everytime I want to call a polymorph
    > member function of the base class (which is a template class).
    >
    > I get an undefined reference error from ld.
    > That's why I start a new post for the more general problem description.


    Okay, the problem was that I have used neither the import nor the export
    model for template source code organization[1].

    I chose the import model and moved the definitions of the template class
    member funtions into the header file and everything is working now.

    [1] http://www.ddj.com/cpp/184401563
    , Nov 15, 2008
    #5
    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.

Share This Page