473,837 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual functions in template class issue

I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.

template<class Type> class base
{
public:
base& operator/=(const base&);
Type *image;
// There is more stuff here, but it is not necessary
for this problem
}

class derived : public base<complex<do uble> >
{
// There is stuff here, but it is not necessary for
this problem
}

template<class Type> base& base<Type>::ope rator/=(const
base& im)
{
for(int i=0; i<loopLength; i++)
{
if(im.image[i] != 0) {
image[i] = image[i] / im.image[i]; }
else {
image[i] = 0; }
}
return *this;
}

Now as that stands everything works fine, as it should.
The problem comes when I try to make the operator/
function a virtual function in base. I want to derive
another class from base that needs a different
implementation of the operator, but derived can use the
same implementation. So, the only change I made to the
code is:

template<class Type> class base
{
public:
virtual base& operator/=(const base&);
Type *image;
// There is more stuff here, but it is not necessary
for this problem
}

It seems to me that this should all still work, but
instead I get 23 strange errors when I try to compile the
code with the virtual function. I should point out that
base and derived are in separate files and these errors
appear when the source for derived compiles. I don't
understand why I get these errors at all. They all come
from the line of code that contains the if(im.image[i] !=
0). I actually have other operators that get the same
errors. They all come from comparisons in if statements
such as if(im.image[i] != image[i]) and they are all
nearly identical except for the operator they list.

I am completely baffled so please help me. Why does it
work completely fine without the virtual?

Chris

The 23 Errors
-----------------------------------------

Image.cpp(500): error C2784: 'bool std::operator !=(const
_Ty &,const std::complex<_T y> &)' : could not deduce
template argument for 'const std::complex<_T y> &'
from 'int'

Image.cpp(500): error C2782: 'bool std::operator !=(const
std::complex<_T y> &,const _Ty &)' : template
parameter '_Ty' is ambiguous

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::complex<_T y> &,const std::complex<_T y> &)' : could
not deduce template argument for 'const std::complex<_T y>
&' from 'int'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_stri ng<_Elem,_Trait s,_Ax> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_stri ng<_Elem,_Trait s,_Ax> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_stri ng<_Elem,_Trait s,_Ax> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
_Elem *,const std::basic_stri ng<_Elem,_Trait s,_Alloc>
&)' : could not deduce template argument for 'const T1 *'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &,const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_stri ng<_Elem,_Trait s,_Ax> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &,const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_stri ng<_Elem,_Trait s,_Ax> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &,const
std::basic_stri ng<_Elem,_Trait s,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_stri ng<_Elem,_Trait s,_Ax> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::allocator< _Ty> &,const std::allocator< _Other> &)' :
could not deduce template argument for 'const
std::allocator< _Ty> &' from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::allocator< _Ty> &,const std::allocator< _Other> &)' :
could not deduce template argument for 'const
std::allocator< _Ty> &' from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::allocator< _Ty> &,const std::allocator< _Other> &)' :
could not deduce template argument for 'const
std::allocator< _Ty> &' from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::istreambuf _iterator<_Elem ,_Traits> &,const
std::istreambuf _iterator<_Elem ,_Traits> &)' : could not
deduce template argument for 'const
std::istreambuf _iterator<_Elem ,_Traits> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::istreambuf _iterator<_Elem ,_Traits> &,const
std::istreambuf _iterator<_Elem ,_Traits> &)' : could not
deduce template argument for 'const
std::istreambuf _iterator<_Elem ,_Traits> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::istreambuf _iterator<_Elem ,_Traits> &,const
std::istreambuf _iterator<_Elem ,_Traits> &)' : could not
deduce template argument for 'const
std::istreambuf _iterator<_Elem ,_Traits> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::reverse_it erator<_RanIt> &,const
std::reverse_it erator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_it erator<_RanIt> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::reverse_it erator<_RanIt> &,const
std::reverse_it erator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_it erator<_RanIt> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::reverse_it erator<_RanIt> &,const
std::reverse_it erator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_it erator<_RanIt> &'
from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::pair<_Ty1, _Ty2> &,const std::pair<_Ty1, _Ty2> &)' :
could not deduce template argument for 'const
std::pair<_Ty1, _Ty2> &' from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::pair<_Ty1, _Ty2> &,const std::pair<_Ty1, _Ty2> &)' :
could not deduce template argument for 'const
std::pair<_Ty1, _Ty2> &' from 'std::complex<d ouble>'

Image.cpp(500): error C2784: 'bool std::operator !=(const
std::pair<_Ty1, _Ty2> &,const std::pair<_Ty1, _Ty2> &)' :
could not deduce template argument for 'const
std::pair<_Ty1, _Ty2> &' from 'std::complex<d ouble>'

Image.cpp(500): error C2676: binary '!
=' : 'std::complex<d ouble>' does not define this operator
or a conversion to a type acceptable to the predefined
operator

Nov 16 '05 #1
3 3942

"Chris" <ch******@ufl.e du> skrev i meddelandet
news:04******** *************** *****@phx.gbl.. .
I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.

template<class Type> class base
{
public:
base& operator/=(const base&);
Type *image;
// There is more stuff here, but it is not necessary
for this problem
}
I guess that in the real code, all the classes are terminated by a
semicolon. :-)

class derived : public base<complex<do uble> >
{
// There is stuff here, but it is not necessary for
this problem
}

template<class Type> base& base<Type>::ope rator/=(const
I think the return value should be

base<Type>&
base& im)
{
for(int i=0; i<loopLength; i++)
{
if(im.image[i] != 0) {
And here is your missing operator!=(), since the complex types cannot
be compared to int. Change it to 0.0 and it will compile.
image[i] = image[i] / im.image[i]; }
else {
image[i] = 0; }
}
return *this;
}


Bo Persson
bo**@telia.com

Nov 16 '05 #2
Also addressed in vc.language and vc.stl.

Please don't multipost. If you must post the same message to multiple
groups, you should crosspost.

--
Craig Powers
MVP - Visual C++
Nov 16 '05 #3
"Chris" <ch******@ufl.e du> wrote in message news:<04******* *************** ******@phx.gbl> ...
I am having a very strange problem involving virtual
functions in template classes. First of all, here is an
extremely simplified structure of the two classes I am
having problems with.

template<class Type> class base
{
public:
base& operator/=(const base&);
Type *image;
// There is more stuff here, but it is not necessary
for this problem
}

class derived : public base<complex<do uble> >
{
// There is stuff here, but it is not necessary for
this problem
}

template<class Type> base& base<Type>::ope rator/=(const
base& im)
This needs to be:
template<class Type> base<Type>& base<Type>::ope rator/=(const
base<Type>& im)
{
for(int i=0; i<loopLength; i++)
{
if(im.image[i] != 0) {
Your use of literal 0 here should be Type(0). As it stands, the
compiler needs to do 2 conversions to turn 0 into a
std::complex<do uble>. First, 0 (an int) needs to be converted to a
double, then the double needs to be converted to a complex<double> .
The compiler won't do 2 conversions, so you need to help it out with
one.
image[i] = image[i] / im.image[i]; }
else {
image[i] = 0; }
}
return *this;
}

[snip]

It seems to me that this should all still work, but
instead I get 23 strange errors when I try to compile the
code with the virtual function. I should point out that
The 23 errors come from the fact that there are 23 operator!=()s in
scope and it's trying all of them looking for a match.

[snip]
I am completely baffled so please help me. Why does it
work completely fine without the virtual?


virtual is a red-herring here. If you eliminate the derived class and
make operator/= non-virtual, the follow will still produce the 23
errors:

int main() {
base<std::compl ex<double> > x;
x /= x;
}

"x /= x" is needed to force the instantiation of base<T>::operat or/=,
where the real problem is. Using virtual also forces that
instantiation so that the compiler can generate the vtables for base
and derived.

Hope this helps,

John
Nov 16 '05 #4

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

Similar topics

2
11333
by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ========================================= Here is some program which is compiled and works fine. ############################################
6
4011
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
5
1992
by: toton | last post by:
Hi, I want a few of my class to overload from a base class, where the base class contains common functionality. This is to avoid repetition of code, and may be reducing amount of code in binary, not to get polymorphic behavior. None of them has virtual methods, and are self contained (no destructor at all) thus do not have a chance to have memory error. Thus the derived classes has additional functionality, not additional data.
3
3331
by: YellowMaple | last post by:
Is it possible to have pure virtual functions defined in a template class? I'm trying to do something like this: template <typename T> class Singleton { public: static T& getInstance(void) { if(m_pInstance == NULL)
14
4234
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
14
2353
by: Hunk | last post by:
Hi I ws wondering if there is a way to implement operator+ in case of virtual classes. Here's the problem. I have to have a base string class from which two classes (normal char string and a hash string class ) are derived. The two derived classes are template classes specifying the sizes. The base class is a non-template class so that it can be used generically in the interface classes. the design would look like
11
2758
by: mathieu | last post by:
Hi there, I don't think I'll be able to describe my issue correctly, so instead I'll just give a pseudo C++ code I am struggling with. Basically I am looking for a 'pure virtual template' function that I would be able to declare in the base class (*). Thanks for suggestions, -Mathieu
2
2417
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
10
1474
by: =?Utf-8?B?Y2FybG0=?= | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
0
9696
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10902
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10583
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
10642
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
9420
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...
0
7014
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
5680
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.