Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Weird template problem: Conflicting return type specified

Reply
Thread Tools

Weird template problem: Conflicting return type specified

 
 
Ton van den Heuvel
Guest
Posts: n/a
 
      04-28-2005
Hi all,

why does the following code not compile, and fail with:

qed.cpp: In instantiation of `B<Foo*>':
qed.cpp:40: instantiated from here
qed.cpp:29: error: conflicting return type specified for `const T
B<T>::Test()
const [with T = Foo*]'
qed.cpp:14: error: overriding `virtual const Foo* A::Test() const'

------------------------

#include <iostream>



struct Foo {
int val;
};

class A {
public:
A( void ) {
}

virtual const Foo* Test( void ) const {
return NULL;
}
protected:
Foo foo;
};



template< class T >
class B : A {
public:
B( void ) {
}

const T Test( void ) const {
return NULL;
}
protected:
T foo;
};



int main( void ) {
A a;
B< Foo* > b;
}


-----------------------

I don't see a problem here. Both MSVC and GCC complain about the code
in a similar way, so there is a fundamental problem here, but I can't
figure out what the problem is. Any hints?

Regards,
Ton van den Heuvel
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-28-2005
Ton van den Heuvel wrote:
> why does the following code not compile, and fail with:
>
> qed.cpp: In instantiation of `B<Foo*>':
> qed.cpp:40: instantiated from here
> qed.cpp:29: error: conflicting return type specified for `const T
> B<T>::Test()
> const [with T = Foo*]'
> qed.cpp:14: error: overriding `virtual const Foo* A::Test() const'


'const T' where 'T' is Foo*

and

'const Foo*'

are different types. The former is actually

'T const'

or, expanded,

'Foo * const'

..

>
> ------------------------
>
> #include <iostream>
>
>
>
> struct Foo {
> int val;
> };
>
> class A {
> public:
> A( void ) {
> }
>
> virtual const Foo* Test( void ) const {
> return NULL;
> }
> protected:
> Foo foo;
> };
>
>
>
> template< class T >
> class B : A {
> public:
> B( void ) {
> }
>
> const T Test( void ) const {
> return NULL;
> }
> protected:
> T foo;
> };
>
>
>
> int main( void ) {
> A a;
> B< Foo* > b;
> }
>
>
> -----------------------
>
> I don't see a problem here.


That happens.

> Both MSVC and GCC complain about the code
> in a similar way, so there is a fundamental problem here, but I can't
> figure out what the problem is. Any hints?


Try to see the difference between

typedef int* pint;
const pint somepointer;

and

const int* somepointer;

This should be enough of a hint (if a hint is what you want).

V
 
Reply With Quote
 
 
 
 
roberth+news@ifi.uio.no
Guest
Posts: n/a
 
      04-28-2005
Ton van den Heuvel <(E-Mail Removed)> wrote:
| Hi all,
|
| why does the following code not compile, and fail with:
|
| qed.cpp: In instantiation of `B<Foo*>':
| qed.cpp:40: instantiated from here
| qed.cpp:29: error: conflicting return type specified for `const T
| B<T>::Test()
| const [with T = Foo*]'
| qed.cpp:14: error: overriding `virtual const Foo* A::Test() const'
|
| ------------------------
|
| #include <iostream>
|
|
|
| struct Foo {
| int val;
| };
|
| class A {
| public:
| A( void ) {
| }
|
| virtual const Foo* Test( void ) const {
| return NULL;
| }
| protected:
| Foo foo;
| };
|
|
|
| template< class T >
| class B : A {
| public:
| B( void ) {
| }
|
| const T Test( void ) const {
| return NULL;
| }
| protected:
| T foo;
| };
|
|
|
| int main( void ) {
| A a;
| B< Foo* > b;
| }
|
|
| -----------------------
|
| I don't see a problem here. Both MSVC and GCC complain about the code
| in a similar way, so there is a fundamental problem here, but I can't
| figure out what the problem is. Any hints?

Yes. Templates are not macros. "const T" with "T = Foo*" is the same as
"Foo * const." That means the pointer is const, not the value pointed to,
and you are trying to override "const Foo* A::Test() const" with "Foo *
const B<Foo*>::Test() const."
--
Robert Bauck Hamar
 
Reply With Quote
 
tonvandenheuvel@gmail.com
Guest
Posts: n/a
 
      05-01-2005
Thanks for the helpful replies. I always considered templates to be
some sort of a macro, I was wrong

 
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
Why is return type in getfullspec().annotations named as "return"? andrew cooke Python 1 04-03-2011 01:02 AM
conflicting declaration in template static member Michael Doubez C++ 11 03-26-2011 02:01 PM
Weird conflicting IP issue Janet Wireless Networking 10 04-19-2008 02:54 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
Can a template function return a template type? Damon C++ 2 12-15-2003 12:56 PM



Advertisments