I have a class "Homework" that has a private attribute "int time" and I have set up a + operator to add the times of two Object instances. I have also created a Template "sum(T a, T b)" that takes two arguments and adds them together.
I'm having difficulty using my "Homework" objects as arguments in my sum() template function. Any obvious reasons why my template breaks?
//code for my template declaration & definition
template <class T>
T sum (T a, T b) {
T result;
result = (a+b);
return (result);
}
and my overloaded + operator definition for class "Homework" looks like this:
//lhs.time + rhs.time = int result
int Homework::operator+ (const Homework rhs) const {
int result;
int a = this->time;
int b = rhs.time;
result = (a+b);
return result;
}
the + overload works fine when used like this:
int total = hw1 + hw2;
cout << total << endl;
// prints sum of hw1.time and hw2.time
... but fails to find the sum when I pass them off to the sum() template:
int total = sum<int> (hw, hw2);
cout << total << endl;
//yields an error!
the error yielded from g++ looks like this:
AddTwoObjects.cpp:69: error: no matching function for call to ‘sum(Homework&, Homework&)’
I've tried changing the template arguments to accept references but that breaks the template functionality for some reason. Can any one point out my mortal c++ sin?
Thanks