Weird template problem: Conflicting return type specified

Discussion in 'C++' started by Ton van den Heuvel, Apr 28, 2005.

  1. 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
    Ton van den Heuvel, Apr 28, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Apr 28, 2005
    #2
    1. Advertising

  3. Ton van den Heuvel

    Guest

    Ton van den Heuvel <> 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
    , Apr 28, 2005
    #3
  4. Ton van den Heuvel

    Guest

    Thanks for the helpful replies. I always considered templates to be
    some sort of a macro, I was wrong :)
    , May 1, 2005
    #4
    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. Damon
    Replies:
    2
    Views:
    11,235
    Damon
    Dec 15, 2003
  2. Greenhorn
    Replies:
    15
    Views:
    796
    Keith Thompson
    Mar 6, 2005
  3. dorayme
    Replies:
    1
    Views:
    602
    richard
    Jan 21, 2011
  4. Michael Doubez
    Replies:
    11
    Views:
    898
    James Kanze
    Mar 26, 2011
  5. will
    Replies:
    6
    Views:
    394
    Phrogz
    Dec 27, 2006
Loading...

Share This Page