operator inheritance from a class template???

J

jmborr

Hi all,
For some reason, my derived class does not inherit the operator '=' of my base class, which is a class template, when I compile with g++
//=========== base class
template<class T>
class vec{
public:
vec &operator=( vec ) ;
};
//=========== derived class
class vec_d : vec<double> {
};
//===========

When I compile, I have the following error:
no match for `vec_d& = vec<double>' operator
candidates are: vec_d& vec_d::eek:perator=(const vec_d&)

Any ideas, please???
jose,
 
P

Pete Becker

jmborr said:
Hi all,
For some reason, my derived class does not inherit the operator '=' of my base class, which is a class template, when I compile with g++
//=========== base class
template<class T>
class vec{
public:
vec &operator=( vec ) ;
};
//=========== derived class
class vec_d : vec<double> {
};
//===========

When I compile, I have the following error:
no match for `vec_d& = vec<double>' operator
candidates are: vec_d& vec_d::eek:perator=(const vec_d&)

When in doubt, simplify. The problem has nothing to do with templates.
Read about assignment operators and inheritance.
 
R

Rolf Magnus

jmborr said:
Hi all,
For some reason, my derived class does not inherit the operator '='
of my base class,

The reason is that operator= is never inherited.
which is a class template, when I compile with g++
//=========== base class template<class T>
class vec{
public:
vec &operator=( vec ) ;

With the above signature, the right hand vec will be copied into the
parameter before it then is copied into the this object. So you shuold
probably use a (const) reference:

vec& operator=(const vec&);
};
//=========== derived class
class vec_d : vec<double> {
};
//===========

Why don't you just typedef it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top