operator= in template derived class
I have a template class
Code:
template<class T>
class WavesetTemplate
{
public:
...
WavesetTemplate<T>& operator=( const WavesetTemplate<T>& );
friend WavesetTemplate<T> operator*(const WavesetTemplate<T>& lhs, const WavesetTemplate<T>& rhs);
protected:
vector<vector<T> > m_TemplateData;
...
};
and derived class:
Code:
class WavesetTime : public WavesetTemplate<double>
{
public:
WavesetTime(){} ;
virtual ~WavesetTime() {} ;
WavesetTime& operator=(const WavesetTime& rhs) { WavesetTemplate<double>::operator=(rhs);return *this;} // call base operator= function
... //other type specific functions
};
This works...
Code:
WavesetTemplate<double> x,y,z;
x.SetWave(0.0,flat10,0.1); //set some data
y.SetWave(0.0,ramp,0.1); //set some data
z=x * y; // this works
BUT THIS
Code:
WavesetTime x,y,z;
x.SetWave(0.0,flat10,0.1); //set some data
y.SetWave(0.0,ramp,0.1); //set some data
z=x * y; // compile error
gives the compile error:
binary '=' : no operator defined which takes a right-hand operand of type 'class WavesetTemplate<double>' (or there is no acceptable conversion).
I also get the same problem when operator* is a member function, rather than here where it is non-member function.
The only way I can get this compile error to disappear is to put operator* into the derived class WavesetTime, which rather defeats the point of templates.
I would be very grateful if anyone could suggest a solution to this problem. Thank you.
Re: operator= in template derived class
Quote:
Originally posted by JonnoA
BUT THIS
Code:
WavesetTime x,y,z;
x.SetWave(0.0,flat10,0.1); //set some data
y.SetWave(0.0,ramp,0.1); //set some data
z=x * y; // compile error
gives the compile error:
binary '=' : no operator defined which takes a right-hand operand of type 'class WavesetTemplate<double>' (or there is no acceptable conversion).
Since operator * is defined in the WavesetTemplate<double>, you are trying to assign a WavesetTemplate<double> to a WavesetTime. There is no operator = defined that does this conversion.
Here is the output from the Comeau compiler (after making a few corrections):
Code:
#include <vector>
using namespace std;
template<class T>
class WavesetTemplate
{
public:
WavesetTemplate<T>& operator=( const WavesetTemplate<T>& );
friend WavesetTemplate<T> operator*(const WavesetTemplate<T>& lhs, const WavesetTemplate<T>& rhs);
protected:
vector<vector<T> > m_TemplateData;
};
class WavesetTime : public WavesetTemplate<double>
{
public:
WavesetTime(){} ;
virtual ~WavesetTime() {} ;
WavesetTime& operator=(const WavesetTime& rhs)
{
WavesetTemplate<double>::operator=(rhs);
return *this;
}
};
void foo()
{
WavesetTemplate<double> x,y,z;
z=x * y; // this works
}
void foo2()
{
WavesetTime x,y,z;
z= x * y; // compile error (Line 36 from Comeau output below)
}
Output:
=============================================
Comeau C/C++ 4.3.3 (Aug 6 2003 15:13:37) for ONLINE_EVALUATION_BETA1
Copyright 1988-2003 Comeau Computing. All rights reserved.
MODE:strict errors C++
"ComeauTest.c", line 36: error: no operator "=" matches these operands
operand types are: WavesetTime = WavesetTemplate<double>
z= x * y; // compile error
^
1 error detected in the compilation of "ComeauTest.c".
Is it necessary to have a user defined operator = and copy constructor for your classes? You didn't post the entire class interface, but a vector<vector<T> > is copyable without a user defined copy constructor / assignment operator.
Regards,
Paul McKenzie