operator = override

J

James Aguilar

I am having a problem with a derived class not seeing an operator in a base
class.

Suppose I have a base class:

template <typename T>
class Array
{
protected:
T *m_array;
public:
//...
Array<T> &operator =(const Array<T> &other);
Array<T> &operator =(const T &other);
//...
};

Then I have this derived class:

class Vector : public Array<double>
{
//...
};

When I try to say

Vector v(20);
v = 3.14;

I receive this error:

lab3-vector.cpp:22: error: no match for 'operator=' in 'vector2 =
3.14000000000000012434497875801753252744674682617e+0'
vector.hpp:11: error: candidates are: Vector& Vector::eek:perator=(const Vector&)

But it should call Array<double> &operator =(const double &d). Can anyone say
why it doesn't?

- JFA1
 
L

Larry Brasfield

James Aguilar said:
I am having a problem with a derived class not seeing an operator in a base class.

Suppose I have a base class:

template <typename T>
class Array
{
protected:
T *m_array;
public:
//...
Array<T> &operator =(const Array<T> &other);
Array<T> &operator =(const T &other);
//...
};

Then I have this derived class:

class Vector : public Array<double>
{
//...
};

When I try to say

Vector v(20);
v = 3.14;

I receive this error:

lab3-vector.cpp:22: error: no match for 'operator=' in 'vector2 =
3.14000000000000012434497875801753252744674682617e+0'
vector.hpp:11: error: candidates are: Vector& Vector::eek:perator=(const Vector&)

But it should call Array<double> &operator =(const double &d). Can anyone say why it doesn't?


The default, compiler-generate assignment operator for
a class T has the signature: T& operator=(const T&);
It performs assignment for the class member and base
objects as defined for those objects. Your assignment
attempts to use the one and only assignment operator
defined for Vector. It cannot compile.
 
N

namespace

James said:
template <typename T>
class Array
{
protected:
T *m_array;
public:
//...
Array<T> &operator =(const Array<T> &other);
Array<T> &operator =(const T &other);
//...
};
class Vector : public Array<double>
{
//...
};

When I try to say

Vector v(20);
v = 3.14;

The compiler generated operator= of class Vector hides the various
Array<double>::eek:perator=. Please see if adding a using declaration
works.

class Vector : public Array<double>
{
typedef Array<double> BaseClass;
public:
using BaseClass::eek:perator=;
//...
};
 

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

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top