Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Weird compile error in conjunction with templates & inheritance

Reply
Thread Tools

Weird compile error in conjunction with templates & inheritance

 
 
Student
Guest
Posts: n/a
 
      12-04-2010
Why is the following code, which used to compile fine some years ago,
not compiling anymore?

g++ test.cpp
test.cpp: In member function ‘int D<T>::Size()’:
test.cpp:35: error: ‘v’ was not declared in this scope
test.cpp:35: error: ‘k’ was not declared in this scope


// test.cpp

template <typename T = int>
class B
{
public:
int v, k;

B(int Av, int Ak)
{
v = Av;
k = Ak;
}

virtual int Size() = 0;

//...
};

template <typename T = int>
class D : public B<T>
{
public:
D(int Av, int Ak) : B<T>(Av, Ak)
{
}

int Size()
{
return v * k;
}
};

int main(int argc, char* argv[])
{
D<long> C(10, 2);
//...
return 0;
}

 
Reply With Quote
 
 
 
 
Student
Guest
Posts: n/a
 
      12-04-2010
[REPOST]

Why is the following code, which used to compile fine some years ago,
not compiling anymore?

g++ test.cpp
test.cpp: In member function ‘int D<T>::Size()’:
test.cpp:35: error: ‘v’ was not declared in this scope
test.cpp:35: error: ‘k’ was not declared in this scope


// test.cpp

template <typename T = int>
class B
{
public:
int v, k;

B(int Av, int Ak)
{
v = Av;
k = Ak;
}

virtual int Size() = 0;

//...
};


template <typename T = int>
class D : public B<T>
{
public:
D(int Av, int Ak) : B<T>(Av, Ak)
{
}

int Size()
{
return v * k;
}
};


int main(int argc, char* argv[])
{
D<long> C(10, 2);
//...
return 0;
}


 
Reply With Quote
 
 
 
 
Student
Guest
Posts: n/a
 
      12-04-2010
On 2010-12-04 09:40, Paavo Helde wrote:
> Student<> wrote in news:idcqa2$19o$1
> @speranza.aioe.org:
>
>> Why is the following code, which used to compile fine some years ago,
>> not compiling anymore?
>>
>> g++ test.cpp
>> test.cpp: In member function ?€?int D<T>::Size()?€™:
>> test.cpp:35: error: ?€?v?€™ was not declared in this scope
>> test.cpp:35: error: ?€?k?€™ was not declared in this scope
>>
>>
>> // test.cpp
>>
>> template<typename T = int>
>> class B
>> {
>> public:
>> int v, k;
>>
>> B(int Av, int Ak)
>> {
>> v = Av;
>> k = Ak;
>> }
>>
>> virtual int Size() = 0;
>>
>> //...
>> };
>>
>> template<typename T = int>
>> class D : public B<T>
>> {
>> public:
>> D(int Av, int Ak) : B<T>(Av, Ak)
>> {
>> }
>>
>> int Size()
>> {
>> return v * k;

>
> The base class depends on the template argument and can be different in
> different specializations, so the compiler cannot be sure what v and k
> really represent. Clarify with:
>
> return this->v * this->k;
>
>
> hth
> Paavo


Hey, cool, thanks, this solves the problem,
although I'm not really convinced why one has to use "this" there.

 
Reply With Quote
 
Student
Guest
Posts: n/a
 
      12-04-2010
On 2010-12-04 09:54, Student wrote:
> On 2010-12-04 09:40, Paavo Helde wrote:
>> Student<> wrote in news:idcqa2$19o$1
>> @speranza.aioe.org:
>>
>>> int Size()
>>> {
>>> return v * k;

>>
>> The base class depends on the template argument and can be different in
>> different specializations, so the compiler cannot be sure what v and k
>> really represent. Clarify with:
>>
>> return this->v * this->k;
>>
>>
>> hth
>> Paavo

>
> Hey, cool, thanks, this solves the problem,
> although I'm not really convinced why one has to use "this" there.


Hmm... it's a nightmare to change all the code.
I think this is clearly a compiler bug.
 
Reply With Quote
 
Marco
Guest
Posts: n/a
 
      12-04-2010
Student <> writes:

> On 2010-12-04 09:54, Student wrote:
>> On 2010-12-04 09:40, Paavo Helde wrote:
>>> Student<> wrote in news:idcqa2$19o$1
>>> @speranza.aioe.org:
>>>
>>>> int Size()
>>>> {
>>>> return v * k;
>>>
>>> The base class depends on the template argument and can be different in
>>> different specializations, so the compiler cannot be sure what v and k
>>> really represent. Clarify with:
>>>
>>> return this->v * this->k;
>>>
>>>
>>> hth
>>> Paavo

>>
>> Hey, cool, thanks, this solves the problem,
>> although I'm not really convinced why one has to use "this" there.

>
> Hmm... it's a nightmare to change all the code.
> I think this is clearly a compiler bug.


No. See e.g. http://www.parashift.com/c++-faq-lite/templates.html
section

[35.19] Why am I getting errors when my template-derived-class uses a
member it inherits from its template-base-class?

for further explanation.

hth
--
m
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      12-04-2010
Hi,

Paavo Helde wrote:
> Also, it seems you are trying to mix templates with OOP and
> virtual methods, this is not a very good design as they both solve
> similar problems, but in different ways which may easily conflict each
> other.


why do you think that these concepts should be used mutually exclusive?

I use templates to support interface implementations from time to time.
This will obviously mix virtual methods and templates.


Marcel
 
Reply With Quote
 
Student
Guest
Posts: n/a
 
      12-04-2010
On 2010-12-04 10:29, Paavo Helde wrote:
> Student<> wrote in news:idd041$bue$1
> @speranza.aioe.org:
>
>> On 2010-12-04 09:54, Student wrote:
>>> On 2010-12-04 09:40, Paavo Helde wrote:
>>>> Student<> wrote in news:idcqa2$19o$1
>>>> @speranza.aioe.org:
>>>>
>>>>> int Size()
>>>>> {
>>>>> return v * k;
>>>>
>>>> The base class depends on the template argument and can be different

> in
>>>> different specializations, so the compiler cannot be sure what v and

> k
>>>> really represent. Clarify with:
>>>>
>>>> return this->v * this->k;
>>>>

>>
>> Hmm... it's a nightmare to change all the code.
>> I think this is clearly a compiler bug.

>
> No, it's not a bug, it's a feature Do not expect this to go away.
>
> Normally there are not so many templated base classes used in the code,
> why do you think you have to change "all the code"? Normally all member
> variables are private, so you would not use them in derived classes
> anyway. Also, it seems you are trying to mix templates with OOP and
> virtual methods, this is not a very good design as they both solve
> similar problems, but in different ways which may easily conflict each
> other. At least be prepared to having some complicated and cumbersome
> code if you insist on using them together.


Since this code compiled fine some years ago,
does anybody know if there is a compiler switch in g++
to get the code compile again without the above ugly "this->" 'workaround'?

g++ --version
g++ (Debian 4.4.4-11) 4.4.5 20100824 (prerelease)
 
Reply With Quote
 
Marc
Guest
Posts: n/a
 
      12-04-2010
Student wrote:

> Since this code compiled fine some years ago,
> does anybody know if there is a compiler switch in g++
> to get the code compile again without the above ugly "this->" 'workaround'?


Not that I know of. Is the solution of using "using" also too ugly for you?
 
Reply With Quote
 
Student
Guest
Posts: n/a
 
      12-05-2010
On 2010-12-04 15:08, Marc wrote:
> Student wrote:
>
>> Since this code compiled fine some years ago,
>> does anybody know if there is a compiler switch in g++
>> to get the code compile again without the above ugly "this->" 'workaround'?

>
> Not that I know of. Is the solution of using "using" also too ugly for you?


To be honest: yes.
Because in the math formulae I use there's of course no such thing
as "this->", so then why should I cripple the code with it?
For me, the use of "this->" makes no sense at all.
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      12-05-2010
On Dec 5, 8:31*pm, Student <inva...@amitrader.com> wrote:
> On 2010-12-04 15:08, Marc wrote:
>
> > Student *wrote:

>
> >> Since this code compiled fine some years ago,
> >> does anybody know if there is a compiler switch in g++
> >> to get the code compile again without the above ugly "this->" 'workaround'?

>
> > Not that I know of. Is the solution of using "using" also too ugly for you?

>
> To be honest: yes.
> Because in the math formulae I use there's of course no such thing
> as "this->", so then why should I cripple the code with it?
> For me, the use of "this->" makes no sense at all.


You perhaps did not understand the question there. It was what is ugly
about that solution:


// test.cpp
template <typename T = int>
class B
{
public:
int v, k;
B( int Av, int Ak )
: v( Av )
, k( Ak )
{}
virtual int Size() = 0;
//...
};

template <typename T = int>
class D : public B<T>
{
public:
using B<T>::v;
using B<T>::k;
D( int Av, int Ak )
: B<T>( Av, Ak )
{}
int Size()
{
return v * k;
}
};

int main(int argc, char* argv[])
{
D<long> C(10, 2);
//...
return 0;
}
 
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
Weird behaviour with templates, virtual inheritance and overloadedmethods Lars Hillebrand C++ 5 11-08-2007 08:04 AM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Using a class as a Singleton in Conjunction with AXIS christian.bongiorno@gmail.com Java 2 11-01-2005 09:41 PM
How to compile C++ program with templates to pure C or C++ program without templates? PengYu.UT@gmail.com C++ 5 10-12-2005 06:47 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57