Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor

Reply
Thread Tools

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

 
 
l.s.rockfan@web.de
Guest
Posts: n/a
 
      11-14-2008
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.
 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      11-14-2008
On Nov 14, 3:30*pm, "(E-Mail Removed)" <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
l.s.rockfan@web.de
Guest
Posts: n/a
 
      11-15-2008
Salt_Peter wrote:
> On Nov 14, 3:30 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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.
 
Reply With Quote
 
l.s.rockfan@web.de
Guest
Posts: n/a
 
      11-15-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Salt_Peter wrote:
>> On Nov 14, 3:30 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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
 
Reply With Quote
 
l.s.rockfan@web.de
Guest
Posts: n/a
 
      11-15-2008
(E-Mail Removed) wrote:
> Salt_Peter wrote:
>> On Nov 14, 3:30 pm, "(E-Mail Removed)" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 2 11-14-2008 10:04 PM
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM



Advertisments