I am having some trouble getting some overloaded operators working correctly with a simple inherited Vector class (numerical vector, not container) The scalar multiply from the base class doesn't appear to be visible to the derived class. Code follows:
Base Class Header
#pragma once
#include <vector>
class Base
{
protected:
std::vector<double> val;
public:
Base();
Base( double v0, double v1, double v2 );
virtual ~Base(){}
virtual Base operator*( const double scalar ) const;
};
Base Class Implementation
#include "base.h"
using namespace std;
Base::Base(){
val = vector<double>( 3, 0.0 );
}
Base::Base( double v0, double v1, double v2 ){
val = vector<double>( 3, 0.0 );
val[0] = v0;
val[1] = v1;
val[2] = v2;
}
Base Base::operator*( const double scalar ) const{
return Base( val[0] * scalar, val[1] * scalar, val[2] * scalar );
}
Derived Class Header
#pragma once
#include "base.h"
class Derived : public Base{
public:
Derived();
Derived( double a, double b, double c );
Derived( const Base &other );
virtual ~Derived(){}
double a() const;
double b() const;
double c() const;
double operator*( const Derived& other ) const;
};
Derived Class Implementation
#include "derived.h"
Derived::Derived() : Base(){}
Derived::Derived( double a, double b, double c ) : Base(a,b,c){}
Derived::Derived( const Base &other ) : Base(other){}
double Derived::a() const{
return val[0];
}
double Derived::b() const{
return val[1];
}
double Derived::c() const{
return val[2];
}
double Derived::operator*( const Derived& other ) const{
return val[0] * other.val[0] + val[1] * other.val[1] + val[2] * other.val[2];
}
Main Program
#include <vector>
#include "base.h"
#include "derived.h"
using namespace std;
int main(int argc, char *argv[])
{
Derived d, e;
e = d * 1.0;
return 0;
}
Compiler Output
main.cpp: In function ‘int main(int, char**)’:
main.cpp:15: error: no match for ‘operator*’ in ‘d * 1.0e+0’
derived.h:15: note: candidates are: double Derived::operator*(const Derived&) const