C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Weird template problem: Conflicting return type specified

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Ton van den Heuvel
Guest





PostPosted: Thu Apr 28, 2005 1:03 pm    Post subject: Weird template problem: Conflicting return type specified Reply with quote



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
Back to top
Victor Bazarov
Guest





PostPosted: Thu Apr 28, 2005 1:16 pm    Post subject: Re: Weird template problem: Conflicting return type specifie Reply with quote



Ton van den Heuvel wrote:
Quote:
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'

..

Quote:

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

#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.

Quote:
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

Back to top
roberth+news@ifi.uio.no
Guest





PostPosted: Thu Apr 28, 2005 1:20 pm    Post subject: Re: Weird template problem: Conflicting return type specifie Reply with quote



Ton van den Heuvel <tonvandenheuvel (AT) gmail (DOT) com> wrote:
Quote:
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

Back to top
tonvandenheuvel@gmail.com
Guest





PostPosted: Sun May 01, 2005 6:48 pm    Post subject: Re: Weird template problem: Conflicting return type specifie Reply with quote

Thanks for the helpful replies. I always considered templates to be
some sort of a macro, I was wrong :)

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.