C
Chris
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<double> >
{
// There is stuff here, but it is not necessary for
this problem
}
template<class Type> base& base<Type>:perator/=(const
base& im)
{
for(int i=0; i<loopLength; i++)
{
if(im.image != 0) {
image = image / im.image; }
else {
image = 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 !=
0). I actually have other operators that get the same
errors. They all come from comparisons in if statements
such as if(im.image != image) 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:perator !=(const
_Ty &,const std::complex<_Ty> &)' : could not deduce
template argument for 'const std::complex<_Ty> &'
from 'int'
Image.cpp(500): error C2782: 'bool std:perator !=(const
std::complex<_Ty> &,const _Ty &)' : template
parameter '_Ty' is ambiguous
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::complex<_Ty> &,const std::complex<_Ty> &)' : could
not deduce template argument for 'const std::complex<_Ty>
&' from 'int'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
_Elem *,const std::basic_string<_Elem,_Traits,_Alloc>
&)' : could not deduce template argument for 'const T1 *'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::allocator<_Ty> &,const std::allocator<_Other> &)' :
could not deduce template argument for 'const
std::allocator<_Ty> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::allocator<_Ty> &,const std::allocator<_Other> &)' :
could not deduce template argument for 'const
std::allocator<_Ty> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::allocator<_Ty> &,const std::allocator<_Other> &)' :
could not deduce template argument for 'const
std::allocator<_Ty> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(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<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(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<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(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<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::reverse_iterator<_RanIt> &,const
std::reverse_iterator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_iterator<_RanIt> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::reverse_iterator<_RanIt> &,const
std::reverse_iterator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_iterator<_RanIt> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::reverse_iterator<_RanIt> &,const
std::reverse_iterator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_iterator<_RanIt> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' :
could not deduce template argument for 'const
std:air<_Ty1,_Ty2> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' :
could not deduce template argument for 'const
std:air<_Ty1,_Ty2> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' :
could not deduce template argument for 'const
std:air<_Ty1,_Ty2> &' from 'std::complex<double>'
Image.cpp(500): error C2676: binary '!
=' : 'std::complex<double>' does not define this operator
or a conversion to a type acceptable to the predefined
operator
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<double> >
{
// There is stuff here, but it is not necessary for
this problem
}
template<class Type> base& base<Type>:perator/=(const
base& im)
{
for(int i=0; i<loopLength; i++)
{
if(im.image != 0) {
image = image / im.image; }
else {
image = 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 !=
0). I actually have other operators that get the same
errors. They all come from comparisons in if statements
such as if(im.image != image) 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:perator !=(const
_Ty &,const std::complex<_Ty> &)' : could not deduce
template argument for 'const std::complex<_Ty> &'
from 'int'
Image.cpp(500): error C2782: 'bool std:perator !=(const
std::complex<_Ty> &,const _Ty &)' : template
parameter '_Ty' is ambiguous
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::complex<_Ty> &,const std::complex<_Ty> &)' : could
not deduce template argument for 'const std::complex<_Ty>
&' from 'int'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem
*)' : could not deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
_Elem *,const std::basic_string<_Elem,_Traits,_Alloc>
&)' : could not deduce template argument for 'const T1 *'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::basic_string<_Elem,_Traits,_Alloc> &,const
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not
deduce template argument for 'const
std::basic_string<_Elem,_Traits,_Ax> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::allocator<_Ty> &,const std::allocator<_Other> &)' :
could not deduce template argument for 'const
std::allocator<_Ty> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::allocator<_Ty> &,const std::allocator<_Other> &)' :
could not deduce template argument for 'const
std::allocator<_Ty> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::allocator<_Ty> &,const std::allocator<_Other> &)' :
could not deduce template argument for 'const
std::allocator<_Ty> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(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<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(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<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(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<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::reverse_iterator<_RanIt> &,const
std::reverse_iterator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_iterator<_RanIt> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::reverse_iterator<_RanIt> &,const
std::reverse_iterator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_iterator<_RanIt> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std::reverse_iterator<_RanIt> &,const
std::reverse_iterator<_RanIt> &)' : could not deduce
template argument for 'const
std::reverse_iterator<_RanIt> &'
from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' :
could not deduce template argument for 'const
std:air<_Ty1,_Ty2> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' :
could not deduce template argument for 'const
std:air<_Ty1,_Ty2> &' from 'std::complex<double>'
Image.cpp(500): error C2784: 'bool std:perator !=(const
std:air<_Ty1,_Ty2> &,const std:air<_Ty1,_Ty2> &)' :
could not deduce template argument for 'const
std:air<_Ty1,_Ty2> &' from 'std::complex<double>'
Image.cpp(500): error C2676: binary '!
=' : 'std::complex<double>' does not define this operator
or a conversion to a type acceptable to the predefined
operator