0

I have a base class with many child classes. How would I implement a template operator over loader in the base class to work for all the inheriting classes? I tried to make one with the + operator but it complained that I had too many parameters. I'm not actually sure this is the right way to go about doing this (I'm just starting to use OOP) so if you can think of a better way that would also be great.

I'm making a library in which each metric space is a class. I want to make a base class "Operations" that every space inherits.

My template base class:

#ifndef __libSpace__Operations__
#define __libSpace__Operations__

template< typename T >
class Operations{
public:
    friend T operator+( const T& sp, const T& nsp ){
        return T(sp.dimension + nsp.dimension);
    };
};

#endif

child:

#ifndef __libSpace__EuclidSP__
#define __libSpace__EuclidSP__

#include "Operations.h"

class EuclidSP: public Operations<EuclidSP>{
public:
    EuclidSP(int n = 0, ...);
    ~EuclidSP();

    double* vector();

private:
    int dimension;
    double *vec = new double(dimension);
};

#endif

main:

#include <iostream>
#include "EuclidSP.h"

int main(int argc, const char * argv[])
{
EuclidSP ob1(3,4.0,5.0,6.0);
EuclidSP ob2(3,2.0,5.0,3.0);
EuclidSP obj3();

obj3 = ob2+ob1;

return 0;
}
jay
  • 477
  • 1
  • 9
  • 17

2 Answers2

1

A member operator +() have only one parameter, the right operand. The left or first is always *this. Depending of your situation, you need just a base +, a virtual + or a template. A free operator +() take two argumets, "left" and "right".

In your code:

template< typename T >
class Operations{
public:
    friend T operator+( const T& sp, const T& nsp ){
        return T(sp.dimension + nsp.dimension);
    };
};

You whant a member or a friend?

If a friend, the problems is that +() have to be define outside the class, it is only a friend, not a member.

template< typename T >
    T operator+( const T& sp, const T& nsp );

template< typename T >
class Operations{
public:
    friend T operator+<T>( const T& sp, const T& nsp );

};

template< typename T >
    T operator+( const T& sp, const T& nsp )
    {
        return T(sp.dimension + nsp.dimension);
    }

BUT !!!! Now you have the REAL problem: +() uses a privat member of the derived class, not the base class, so it need to be a friend of the derived class. I think you need to rethink ;-) your design. If you are so confortable using dimension in Operations.. could it be a protected member of Operations??? All your Operations have an dimension?

qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
0

Your base class should know the derived types class by receiving it as a template parameter and implement operator+ as friend:

template< typename T >
class base
{
    friend T operator+( const T& lhs, const T& rhs )
    {
       // your generic implementation here...
    }
};

the derived classes then derive from the base class like this:

class D1 : public base<D1> { ... };
class D2 : public base<D2> { ... };
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • I get the error "Overloaded 'operator+' must be a unary or binary operator (has 3 parameters)" from this code – jay Feb 20 '13 at 23:44
  • Which compiler and version are you using? – Daniel Frey Feb 20 '13 at 23:52
  • I take it `g++ --version` returns something like `i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)`? Because that's what I just tested and the code works fine. Can you edit your question and post your complete example? – Daniel Frey Feb 21 '13 at 00:23
  • Okay, I put in the rest of the class. Let me know if you want the child class as well for any reason. – jay Feb 21 '13 at 00:34
  • Please, post the _complete_ example (after making it as short as possible, of course, but including `#include`'s, `main()`, etc.) Don't let others guess. – Daniel Frey Feb 21 '13 at 00:41
  • The code has several obvious problems and can't compile, your compiler should complain about that and not about operator+. 1) `dimension` is private, so operator+ can't access it. 2) `vec` is initialized in a totally illegal way. 3) The dtor of `EuclidSP` is declared but never defined. 4) `obj3` is a function declaration and not a variable. Sorry, but I will stop wasting my time on this. – Daniel Frey Feb 21 '13 at 01:01
  • Ya sorry that is why I hadn't originally posted everything. I'm aware of the other errors, but those are all easily fixed. The one I don't know is the one I asked about. – jay Feb 21 '13 at 01:04