I'm working on the C++ project where I have to overload operands in a generic class (template class).
So, I have a sample class like this:
template <class T>
class SomeClass {
private:
T variable;
public:
explicit SomeClass(T variable) : variable(variable) {}
SomeClass operator + (SomeClass const &someClass) {
return SomeClass<T>(this->variable + someClass.variable);
}
};
And main method like this:
int main() {
SomeClass<int>* obj1 = new SomeClass<int>(5);
SomeClass<int>* obj2 = new SomeClass<int>(7);
SomeClass<int>* result = obj1 + obj2;
return 0;
}
If I hover the "+" sign, I'm seeing this error message:
Invalid operands to binary expression ('SomeClass<int> *' and 'SomeClass<int> *')
What is the cause of this error?
If I don't use pointers, everything works fine. (when I write smth like this):
int main() {
SomeClass<int> obj1(5);
SomeClass<int> obj2(7);
SomeClass<int> result = obj1 + obj2;
return 0;
}
Note: I have to use pointers in my app, so the last code snippet isn't viable for me.