473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Weird template problem: Conflicting return type specified

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
Jul 23 '05 #1
3 5566
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
Jul 23 '05 #2
Ton van den Heuvel <to*************@gmail.com> 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
Jul 23 '05 #3
Thanks for the helpful replies. I always considered templates to be
some sort of a macro, I was wrong :)

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
4
by: Ondrej Spanel | last post by:
The code below does not compile with .NET 2003, I get folowing error: w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' : illegal type for non-type template parameter 'x' ...
5
by: christian | last post by:
Hi! I have a problem with a template function im MSVC6 the template function is defined as: template <__Type1, __Type2> int MyFunc(int param1, double param2) {__Type1 var1; __Type2 var2; ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
8
by: nickyeng | last post by:
I have written 3 files, i dont know whether i do it correctly or wrongly but somehow it compiled well and can run. My simple aim is to display the terrain.txt file into the terrain array, and then...
6
by: gasfusion | last post by:
I wrote a class Min-Max Heap Template Class which works perfectly fine with integers. As part of this data structure, i have to implement some sort of method to check for the smallest...
4
by: Alan Woodland | last post by:
I've been trying out more template metaprogramming ideas with typelists (mostly for personal learning, I'm aware boost most probably provides this facility already), and I've run into this small...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
2
by: TamaThps | last post by:
Hi, I'm using visual studio 2008 and normally when I get an error it shows what line it is on and which file etc. The error I'm getting I don't know how to solve or even what the problem is. This...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.