473,779 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5591
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
2086
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 something like below: class PMinimum { public: template <typename T> bool operator()(const T & a, const T & b)
4
4035
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' The error shows compiler is quite confused - x is not a template parameter at all. I have found two workarounds, but still I think the code should compile.
5
1944
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; ... do something ...
19
7937
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 macro: #define min(a,b) ((a<b)?a:b) I thought that another way could be to use a template function: template <class T> T min<T a, T b>
8
2213
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 display terrain into screen. I can't display the whitespace characters from terrain.txt file to screen. and It display "weird"( the red color part). http://i19.photobucket.com/albums/b171/NickyEng/display.jpg I tried searching one line by...
6
2271
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 children/grandchildren of any given position in the Min-Max Heap array. Part of this method retrieves an item from my MinMaxHeap array at a specific position and then compare it to NULL. This works fine with integers. However i cannot(!) compare strings to NULL. I...
4
1903
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 problem here. Comeau online (without C++0x, in strict mode) accepts this example pasted here, (apologies for the length of it). Unfortunately G++ (3.4, 4.1, 4.2) doesn't, and complains about index_find being private. The exact error it gives is:...
8
284
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
1974
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 is the whole output when I try to run the program. "1>------ Build started: Project: corysid, Configuration: Debug Win32 ------ 1>Compiling... 1>implementation.cpp 1>Linking... 1>actualmain.obj : error LNK2019: unresolved external symbol "public:...
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.