I am trying to write a C++ program which wraps numeric values, I am doing this by writing a super class which will handle two simple functions, and an operator overloading function. This is my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <class T>
class Number {
protected:
T number;
public:
Number(T num) {
number = num;
}
string mytype() {
return typeid(number).name();
}
string what_am_i() {
ostringstream oss;
oss << "I am " << Number<T>::mytype() << " and my nanana is " << number;
return oss.str();
}
Number operator+ (Number an) {
Number brandNew = NULL;
brandNew.number = number + an.number;
return brandNew;
}
};
class MyInt : public Number<int> {
public:
MyInt() : Number<int>(0){};
MyInt(int num) : Number(num){
}
};
In the Main function I would like to do something like:
void main() {
MyInt three = 3;
MyInt two = 2;
MyInt five = three + two;
cout << five.what_am_i();
}
My problem is the addition between three and two, the compiler says:
no suitable user-defined conversion from "Number" to "MyInt" exists
I could solve this by implementing the overloading function in MyInt but since i want to support many classes like MyShort and MyFloat I would like to leave it in the Superclass. Is there any solution? Thanks!