Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

operator= in template derived class

Status
Not open for further replies.

JonnoA

Programmer
Jan 19, 2004
7
GB
I have a template class

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:

class WavesetTime : public WavesetTemplate<double>
{
public:
WavesetTime(){} ;
virtual ~WavesetTime() {} ;
WavesetTime& operator=(const WavesetTime& rhs) { WavesetTemplate<double>::eek:perator=(rhs);return *this;} // call base operator= function
... //other type specific functions
};

This works...
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
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.
 
you should take in consideration what operator = is not inherited.

Ion Filipski
1c.bmp
 
Thanks. I am aware operator=, copy constructor etc are not inherited, hence in the derived class I am calling the base class' operator=. Even without doing that a default operator= should be called in the derived class which would do something, but I would not expect a compile error.
I am thinking that there is a name resolution problem here - in the second example (Z=x*y), the compiler will do operator* first and returns a WavesetTemplate<double>. It then runs into problems because Z is of type WavesetTime (derived from WavesetTemplate<double>) and operator* has just returned a WavesetTemplate<double>, and it throws the error.
FYI, z=x compiles without problems.
Any further advice would be gratefully received....
 
Have you written above:
.....
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).
.....

??
You still don't believe me? What you expect is not the reality.

Ion Filipski
1c.bmp
 
Thanks again for your quick reply. However, I have implemented an operator= function in BOTH the template AND the derived class (see below). Perhaps my question should then be, what is wrong with my declaration of operator= in the base template and derived class?
In this case I do not actually 'believe' the compiler error - I think I have reached one of its limitations, and would be grateful if someone could offer a work-around, or correct me if I have made a silly error.
Further, if there was a problem with the operator= in the derived class(WavesetTime), why would it then compile when operator* was moved into the derived class?

(extract from above code)
template<class T>
class WavesetTemplate
{
...
WavesetTemplate<T>& operator=( const WavesetTemplate<T>& );
...
};

class WavesetTime : public WavesetTemplate<double>
{
...
WavesetTime& operator=(const WavesetTime& rhs) { WavesetTemplate<double>::eek:perator=(rhs);return *this;} // call operator= in the base class
...
};
 
you need an
operator = (WavesetTemplate<double>....)

Ion Filipski
1c.bmp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top